1 #include <petsc/private/petscfeimpl.h> /*I "petscfe.h" I*/ 2 #include <petscdmplex.h> 3 #include <petscblaslapack.h> 4 5 PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM, PetscInt, PetscInt, PetscBool, PetscInt *, PetscInt *[]); 6 7 struct _n_Petsc1DNodeFamily 8 { 9 PetscInt refct; 10 PetscDTNodeType nodeFamily; 11 PetscReal gaussJacobiExp; 12 PetscInt nComputed; 13 PetscReal **nodesets; 14 PetscBool endpoints; 15 }; 16 17 /* users set node families for PETSCDUALSPACELAGRANGE with just the inputs to this function, but internally we create 18 * an object that can cache the computations across multiple dual spaces */ 19 static PetscErrorCode Petsc1DNodeFamilyCreate(PetscDTNodeType family, PetscReal gaussJacobiExp, PetscBool endpoints, Petsc1DNodeFamily *nf) 20 { 21 Petsc1DNodeFamily f; 22 PetscErrorCode ierr; 23 24 PetscFunctionBegin; 25 ierr = PetscNew(&f);CHKERRQ(ierr); 26 switch (family) { 27 case PETSCDTNODES_GAUSSJACOBI: 28 case PETSCDTNODES_EQUISPACED: 29 f->nodeFamily = family; 30 break; 31 default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unknown 1D node family"); 32 } 33 f->endpoints = endpoints; 34 f->gaussJacobiExp = 0.; 35 if (family == PETSCDTNODES_GAUSSJACOBI) { 36 PetscCheckFalse(gaussJacobiExp <= -1.,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Gauss-Jacobi exponent must be > -1."); 37 f->gaussJacobiExp = gaussJacobiExp; 38 } 39 f->refct = 1; 40 *nf = f; 41 PetscFunctionReturn(0); 42 } 43 44 static PetscErrorCode Petsc1DNodeFamilyReference(Petsc1DNodeFamily nf) 45 { 46 PetscFunctionBegin; 47 if (nf) nf->refct++; 48 PetscFunctionReturn(0); 49 } 50 51 static PetscErrorCode Petsc1DNodeFamilyDestroy(Petsc1DNodeFamily *nf) 52 { 53 PetscInt i, nc; 54 PetscErrorCode ierr; 55 56 PetscFunctionBegin; 57 if (!(*nf)) PetscFunctionReturn(0); 58 if (--(*nf)->refct > 0) { 59 *nf = NULL; 60 PetscFunctionReturn(0); 61 } 62 nc = (*nf)->nComputed; 63 for (i = 0; i < nc; i++) { 64 ierr = PetscFree((*nf)->nodesets[i]);CHKERRQ(ierr); 65 } 66 ierr = PetscFree((*nf)->nodesets);CHKERRQ(ierr); 67 ierr = PetscFree(*nf);CHKERRQ(ierr); 68 *nf = NULL; 69 PetscFunctionReturn(0); 70 } 71 72 static PetscErrorCode Petsc1DNodeFamilyGetNodeSets(Petsc1DNodeFamily f, PetscInt degree, PetscReal ***nodesets) 73 { 74 PetscInt nc; 75 PetscErrorCode ierr; 76 77 PetscFunctionBegin; 78 nc = f->nComputed; 79 if (degree >= nc) { 80 PetscInt i, j; 81 PetscReal **new_nodesets; 82 PetscReal *w; 83 84 ierr = PetscMalloc1(degree + 1, &new_nodesets);CHKERRQ(ierr); 85 ierr = PetscArraycpy(new_nodesets, f->nodesets, nc);CHKERRQ(ierr); 86 ierr = PetscFree(f->nodesets);CHKERRQ(ierr); 87 f->nodesets = new_nodesets; 88 ierr = PetscMalloc1(degree + 1, &w);CHKERRQ(ierr); 89 for (i = nc; i < degree + 1; i++) { 90 ierr = PetscMalloc1(i + 1, &(f->nodesets[i]));CHKERRQ(ierr); 91 if (!i) { 92 f->nodesets[i][0] = 0.5; 93 } else { 94 switch (f->nodeFamily) { 95 case PETSCDTNODES_EQUISPACED: 96 if (f->endpoints) { 97 for (j = 0; j <= i; j++) f->nodesets[i][j] = (PetscReal) j / (PetscReal) i; 98 } else { 99 /* these nodes are at the centroids of the small simplices created by the equispaced nodes that include 100 * the endpoints */ 101 for (j = 0; j <= i; j++) f->nodesets[i][j] = ((PetscReal) j + 0.5) / ((PetscReal) i + 1.); 102 } 103 break; 104 case PETSCDTNODES_GAUSSJACOBI: 105 if (f->endpoints) { 106 ierr = PetscDTGaussLobattoJacobiQuadrature(i + 1, 0., 1., f->gaussJacobiExp, f->gaussJacobiExp, f->nodesets[i], w);CHKERRQ(ierr); 107 } else { 108 ierr = PetscDTGaussJacobiQuadrature(i + 1, 0., 1., f->gaussJacobiExp, f->gaussJacobiExp, f->nodesets[i], w);CHKERRQ(ierr); 109 } 110 break; 111 default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unknown 1D node family"); 112 } 113 } 114 } 115 ierr = PetscFree(w);CHKERRQ(ierr); 116 f->nComputed = degree + 1; 117 } 118 *nodesets = f->nodesets; 119 PetscFunctionReturn(0); 120 } 121 122 /* http://arxiv.org/abs/2002.09421 for details */ 123 static PetscErrorCode PetscNodeRecursive_Internal(PetscInt dim, PetscInt degree, PetscReal **nodesets, PetscInt tup[], PetscReal node[]) 124 { 125 PetscReal w; 126 PetscInt i, j; 127 PetscErrorCode ierr; 128 129 PetscFunctionBeginHot; 130 w = 0.; 131 if (dim == 1) { 132 node[0] = nodesets[degree][tup[0]]; 133 node[1] = nodesets[degree][tup[1]]; 134 } else { 135 for (i = 0; i < dim + 1; i++) node[i] = 0.; 136 for (i = 0; i < dim + 1; i++) { 137 PetscReal wi = nodesets[degree][degree-tup[i]]; 138 139 for (j = 0; j < dim+1; j++) tup[dim+1+j] = tup[j+(j>=i)]; 140 ierr = PetscNodeRecursive_Internal(dim-1,degree-tup[i],nodesets,&tup[dim+1],&node[dim+1]);CHKERRQ(ierr); 141 for (j = 0; j < dim+1; j++) node[j+(j>=i)] += wi * node[dim+1+j]; 142 w += wi; 143 } 144 for (i = 0; i < dim+1; i++) node[i] /= w; 145 } 146 PetscFunctionReturn(0); 147 } 148 149 /* compute simplex nodes for the biunit simplex from the 1D node family */ 150 static PetscErrorCode Petsc1DNodeFamilyComputeSimplexNodes(Petsc1DNodeFamily f, PetscInt dim, PetscInt degree, PetscReal points[]) 151 { 152 PetscInt *tup; 153 PetscInt k; 154 PetscInt npoints; 155 PetscReal **nodesets = NULL; 156 PetscInt worksize; 157 PetscReal *nodework; 158 PetscInt *tupwork; 159 PetscErrorCode ierr; 160 161 PetscFunctionBegin; 162 PetscCheckFalse(dim < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must have non-negative dimension"); 163 PetscCheckFalse(degree < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must have non-negative degree"); 164 if (!dim) PetscFunctionReturn(0); 165 ierr = PetscCalloc1(dim+2, &tup);CHKERRQ(ierr); 166 k = 0; 167 ierr = PetscDTBinomialInt(degree + dim, dim, &npoints);CHKERRQ(ierr); 168 ierr = Petsc1DNodeFamilyGetNodeSets(f, degree, &nodesets);CHKERRQ(ierr); 169 worksize = ((dim + 2) * (dim + 3)) / 2; 170 ierr = PetscMalloc2(worksize, &nodework, worksize, &tupwork);CHKERRQ(ierr); 171 /* loop over the tuples of length dim with sum at most degree */ 172 for (k = 0; k < npoints; k++) { 173 PetscInt i; 174 175 /* turn thm into tuples of length dim + 1 with sum equal to degree (barycentric indice) */ 176 tup[0] = degree; 177 for (i = 0; i < dim; i++) { 178 tup[0] -= tup[i+1]; 179 } 180 switch(f->nodeFamily) { 181 case PETSCDTNODES_EQUISPACED: 182 /* compute equispaces nodes on the unit reference triangle */ 183 if (f->endpoints) { 184 for (i = 0; i < dim; i++) { 185 points[dim*k + i] = (PetscReal) tup[i+1] / (PetscReal) degree; 186 } 187 } else { 188 for (i = 0; i < dim; i++) { 189 /* these nodes are at the centroids of the small simplices created by the equispaced nodes that include 190 * the endpoints */ 191 points[dim*k + i] = ((PetscReal) tup[i+1] + 1./(dim+1.)) / (PetscReal) (degree + 1.); 192 } 193 } 194 break; 195 default: 196 /* compute equispaces nodes on the barycentric reference triangle (the trace on the first dim dimensions are the 197 * unit reference triangle nodes */ 198 for (i = 0; i < dim + 1; i++) tupwork[i] = tup[i]; 199 ierr = PetscNodeRecursive_Internal(dim, degree, nodesets, tupwork, nodework);CHKERRQ(ierr); 200 for (i = 0; i < dim; i++) points[dim*k + i] = nodework[i + 1]; 201 break; 202 } 203 ierr = PetscDualSpaceLatticePointLexicographic_Internal(dim, degree, &tup[1]);CHKERRQ(ierr); 204 } 205 /* map from unit simplex to biunit simplex */ 206 for (k = 0; k < npoints * dim; k++) points[k] = points[k] * 2. - 1.; 207 ierr = PetscFree2(nodework, tupwork);CHKERRQ(ierr); 208 ierr = PetscFree(tup);CHKERRQ(ierr); 209 PetscFunctionReturn(0); 210 } 211 212 /* If we need to get the dofs from a mesh point, or add values into dofs at a mesh point, and there is more than one dof 213 * on that mesh point, we have to be careful about getting/adding everything in the right place. 214 * 215 * With nodal dofs like PETSCDUALSPACELAGRANGE makes, the general approach to calculate the value of dofs associate 216 * with a node A is 217 * - transform the node locations x(A) by the map that takes the mesh point to its reorientation, x' = phi(x(A)) 218 * - figure out which node was originally at the location of the transformed point, A' = idx(x') 219 * - if the dofs are not scalars, figure out how to represent the transformed dofs in terms of the basis 220 * of dofs at A' (using pushforward/pullback rules) 221 * 222 * The one sticky point with this approach is the "A' = idx(x')" step: trying to go from real valued coordinates 223 * back to indices. I don't want to rely on floating point tolerances. Additionally, PETSCDUALSPACELAGRANGE may 224 * eventually support quasi-Lagrangian dofs, which could involve quadrature at multiple points, so the location "x(A)" 225 * would be ambiguous. 226 * 227 * So each dof gets an integer value coordinate (nodeIdx in the structure below). The choice of integer coordinates 228 * is somewhat arbitrary, as long as all of the relevant symmetries of the mesh point correspond to *permutations* of 229 * the integer coordinates, which do not depend on numerical precision. 230 * 231 * So 232 * 233 * - DMPlexGetTransitiveClosure_Internal() tells me how an orientation turns into a permutation of the vertices of a 234 * mesh point 235 * - The permutation of the vertices, and the nodeIdx values assigned to them, tells what permutation in index space 236 * is associated with the orientation 237 * - I uses that permutation to get xi' = phi(xi(A)), the integer coordinate of the transformed dof 238 * - I can without numerical issues compute A' = idx(xi') 239 * 240 * Here are some examples of how the process works 241 * 242 * - With a triangle: 243 * 244 * The triangle has the following integer coordinates for vertices, taken from the barycentric triangle 245 * 246 * closure order 2 247 * nodeIdx (0,0,1) 248 * \ 249 * + 250 * |\ 251 * | \ 252 * | \ 253 * | \ closure order 1 254 * | \ / nodeIdx (0,1,0) 255 * +-----+ 256 * \ 257 * closure order 0 258 * nodeIdx (1,0,0) 259 * 260 * If I do DMPlexGetTransitiveClosure_Internal() with orientation 1, the vertices would appear 261 * in the order (1, 2, 0) 262 * 263 * If I list the nodeIdx of each vertex in closure order for orientation 0 (0, 1, 2) and orientation 1 (1, 2, 0), I 264 * see 265 * 266 * orientation 0 | orientation 1 267 * 268 * [0] (1,0,0) [1] (0,1,0) 269 * [1] (0,1,0) [2] (0,0,1) 270 * [2] (0,0,1) [0] (1,0,0) 271 * A B 272 * 273 * In other words, B is the result of a row permutation of A. But, there is also 274 * a column permutation that accomplishes the same result, (2,0,1). 275 * 276 * So if a dof has nodeIdx coordinate (a,b,c), after the transformation its nodeIdx coordinate 277 * is (c,a,b), and the transformed degree of freedom will be a linear combination of dofs 278 * that originally had coordinate (c,a,b). 279 * 280 * - With a quadrilateral: 281 * 282 * The quadrilateral has the following integer coordinates for vertices, taken from concatenating barycentric 283 * coordinates for two segments: 284 * 285 * closure order 3 closure order 2 286 * nodeIdx (1,0,0,1) nodeIdx (0,1,0,1) 287 * \ / 288 * +----+ 289 * | | 290 * | | 291 * +----+ 292 * / \ 293 * closure order 0 closure order 1 294 * nodeIdx (1,0,1,0) nodeIdx (0,1,1,0) 295 * 296 * If I do DMPlexGetTransitiveClosure_Internal() with orientation 1, the vertices would appear 297 * in the order (1, 2, 3, 0) 298 * 299 * If I list the nodeIdx of each vertex in closure order for orientation 0 (0, 1, 2, 3) and 300 * orientation 1 (1, 2, 3, 0), I see 301 * 302 * orientation 0 | orientation 1 303 * 304 * [0] (1,0,1,0) [1] (0,1,1,0) 305 * [1] (0,1,1,0) [2] (0,1,0,1) 306 * [2] (0,1,0,1) [3] (1,0,0,1) 307 * [3] (1,0,0,1) [0] (1,0,1,0) 308 * A B 309 * 310 * The column permutation that accomplishes the same result is (3,2,0,1). 311 * 312 * So if a dof has nodeIdx coordinate (a,b,c,d), after the transformation its nodeIdx coordinate 313 * is (d,c,a,b), and the transformed degree of freedom will be a linear combination of dofs 314 * that originally had coordinate (d,c,a,b). 315 * 316 * Previously PETSCDUALSPACELAGRANGE had hardcoded symmetries for the triangle and quadrilateral, 317 * but this approach will work for any polytope, such as the wedge (triangular prism). 318 */ 319 struct _n_PetscLagNodeIndices 320 { 321 PetscInt refct; 322 PetscInt nodeIdxDim; 323 PetscInt nodeVecDim; 324 PetscInt nNodes; 325 PetscInt *nodeIdx; /* for each node an index of size nodeIdxDim */ 326 PetscReal *nodeVec; /* for each node a vector of size nodeVecDim */ 327 PetscInt *perm; /* if these are vertices, perm takes DMPlex point index to closure order; 328 if these are nodes, perm lists nodes in index revlex order */ 329 }; 330 331 /* this is just here so I can access the values in tests/ex1.c outside the library */ 332 PetscErrorCode PetscLagNodeIndicesGetData_Internal(PetscLagNodeIndices ni, PetscInt *nodeIdxDim, PetscInt *nodeVecDim, PetscInt *nNodes, const PetscInt *nodeIdx[], const PetscReal *nodeVec[]) 333 { 334 PetscFunctionBegin; 335 *nodeIdxDim = ni->nodeIdxDim; 336 *nodeVecDim = ni->nodeVecDim; 337 *nNodes = ni->nNodes; 338 *nodeIdx = ni->nodeIdx; 339 *nodeVec = ni->nodeVec; 340 PetscFunctionReturn(0); 341 } 342 343 static PetscErrorCode PetscLagNodeIndicesReference(PetscLagNodeIndices ni) 344 { 345 PetscFunctionBegin; 346 if (ni) ni->refct++; 347 PetscFunctionReturn(0); 348 } 349 350 static PetscErrorCode PetscLagNodeIndicesDuplicate(PetscLagNodeIndices ni, PetscLagNodeIndices *niNew) 351 { 352 PetscErrorCode ierr; 353 354 PetscFunctionBegin; 355 ierr = PetscNew(niNew);CHKERRQ(ierr); 356 (*niNew)->refct = 1; 357 (*niNew)->nodeIdxDim = ni->nodeIdxDim; 358 (*niNew)->nodeVecDim = ni->nodeVecDim; 359 (*niNew)->nNodes = ni->nNodes; 360 ierr = PetscMalloc1(ni->nNodes * ni->nodeIdxDim, &((*niNew)->nodeIdx));CHKERRQ(ierr); 361 ierr = PetscArraycpy((*niNew)->nodeIdx, ni->nodeIdx, ni->nNodes * ni->nodeIdxDim);CHKERRQ(ierr); 362 ierr = PetscMalloc1(ni->nNodes * ni->nodeVecDim, &((*niNew)->nodeVec));CHKERRQ(ierr); 363 ierr = PetscArraycpy((*niNew)->nodeVec, ni->nodeVec, ni->nNodes * ni->nodeVecDim);CHKERRQ(ierr); 364 (*niNew)->perm = NULL; 365 PetscFunctionReturn(0); 366 } 367 368 static PetscErrorCode PetscLagNodeIndicesDestroy(PetscLagNodeIndices *ni) 369 { 370 PetscErrorCode ierr; 371 372 PetscFunctionBegin; 373 if (!(*ni)) PetscFunctionReturn(0); 374 if (--(*ni)->refct > 0) { 375 *ni = NULL; 376 PetscFunctionReturn(0); 377 } 378 ierr = PetscFree((*ni)->nodeIdx);CHKERRQ(ierr); 379 ierr = PetscFree((*ni)->nodeVec);CHKERRQ(ierr); 380 ierr = PetscFree((*ni)->perm);CHKERRQ(ierr); 381 ierr = PetscFree(*ni);CHKERRQ(ierr); 382 *ni = NULL; 383 PetscFunctionReturn(0); 384 } 385 386 /* The vertices are given nodeIdx coordinates (e.g. the corners of the barycentric triangle). Those coordinates are 387 * in some other order, and to understand the effect of different symmetries, we need them to be in closure order. 388 * 389 * If sortIdx is PETSC_FALSE, the coordinates are already in revlex order, otherwise we must sort them 390 * to that order before we do the real work of this function, which is 391 * 392 * - mark the vertices in closure order 393 * - sort them in revlex order 394 * - use the resulting permutation to list the vertex coordinates in closure order 395 */ 396 static PetscErrorCode PetscLagNodeIndicesComputeVertexOrder(DM dm, PetscLagNodeIndices ni, PetscBool sortIdx) 397 { 398 PetscInt v, w, vStart, vEnd, c, d; 399 PetscInt nVerts; 400 PetscInt closureSize = 0; 401 PetscInt *closure = NULL; 402 PetscInt *closureOrder; 403 PetscInt *invClosureOrder; 404 PetscInt *revlexOrder; 405 PetscInt *newNodeIdx; 406 PetscInt dim; 407 Vec coordVec; 408 const PetscScalar *coords; 409 PetscErrorCode ierr; 410 411 PetscFunctionBegin; 412 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 413 ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 414 nVerts = vEnd - vStart; 415 ierr = PetscMalloc1(nVerts, &closureOrder);CHKERRQ(ierr); 416 ierr = PetscMalloc1(nVerts, &invClosureOrder);CHKERRQ(ierr); 417 ierr = PetscMalloc1(nVerts, &revlexOrder);CHKERRQ(ierr); 418 if (sortIdx) { /* bubble sort nodeIdx into revlex order */ 419 PetscInt nodeIdxDim = ni->nodeIdxDim; 420 PetscInt *idxOrder; 421 422 ierr = PetscMalloc1(nVerts * nodeIdxDim, &newNodeIdx);CHKERRQ(ierr); 423 ierr = PetscMalloc1(nVerts, &idxOrder);CHKERRQ(ierr); 424 for (v = 0; v < nVerts; v++) idxOrder[v] = v; 425 for (v = 0; v < nVerts; v++) { 426 for (w = v + 1; w < nVerts; w++) { 427 const PetscInt *iv = &(ni->nodeIdx[idxOrder[v] * nodeIdxDim]); 428 const PetscInt *iw = &(ni->nodeIdx[idxOrder[w] * nodeIdxDim]); 429 PetscInt diff = 0; 430 431 for (d = nodeIdxDim - 1; d >= 0; d--) if ((diff = (iv[d] - iw[d]))) break; 432 if (diff > 0) { 433 PetscInt swap = idxOrder[v]; 434 435 idxOrder[v] = idxOrder[w]; 436 idxOrder[w] = swap; 437 } 438 } 439 } 440 for (v = 0; v < nVerts; v++) { 441 for (d = 0; d < nodeIdxDim; d++) { 442 newNodeIdx[v * ni->nodeIdxDim + d] = ni->nodeIdx[idxOrder[v] * nodeIdxDim + d]; 443 } 444 } 445 ierr = PetscFree(ni->nodeIdx);CHKERRQ(ierr); 446 ni->nodeIdx = newNodeIdx; 447 newNodeIdx = NULL; 448 ierr = PetscFree(idxOrder);CHKERRQ(ierr); 449 } 450 ierr = DMPlexGetTransitiveClosure(dm, 0, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 451 c = closureSize - nVerts; 452 for (v = 0; v < nVerts; v++) closureOrder[v] = closure[2 * (c + v)] - vStart; 453 for (v = 0; v < nVerts; v++) invClosureOrder[closureOrder[v]] = v; 454 ierr = DMPlexRestoreTransitiveClosure(dm, 0, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 455 ierr = DMGetCoordinatesLocal(dm, &coordVec);CHKERRQ(ierr); 456 ierr = VecGetArrayRead(coordVec, &coords);CHKERRQ(ierr); 457 /* bubble sort closure vertices by coordinates in revlex order */ 458 for (v = 0; v < nVerts; v++) revlexOrder[v] = v; 459 for (v = 0; v < nVerts; v++) { 460 for (w = v + 1; w < nVerts; w++) { 461 const PetscScalar *cv = &coords[closureOrder[revlexOrder[v]] * dim]; 462 const PetscScalar *cw = &coords[closureOrder[revlexOrder[w]] * dim]; 463 PetscReal diff = 0; 464 465 for (d = dim - 1; d >= 0; d--) if ((diff = PetscRealPart(cv[d] - cw[d])) != 0.) break; 466 if (diff > 0.) { 467 PetscInt swap = revlexOrder[v]; 468 469 revlexOrder[v] = revlexOrder[w]; 470 revlexOrder[w] = swap; 471 } 472 } 473 } 474 ierr = VecRestoreArrayRead(coordVec, &coords);CHKERRQ(ierr); 475 ierr = PetscMalloc1(ni->nodeIdxDim * nVerts, &newNodeIdx);CHKERRQ(ierr); 476 /* reorder nodeIdx to be in closure order */ 477 for (v = 0; v < nVerts; v++) { 478 for (d = 0; d < ni->nodeIdxDim; d++) { 479 newNodeIdx[revlexOrder[v] * ni->nodeIdxDim + d] = ni->nodeIdx[v * ni->nodeIdxDim + d]; 480 } 481 } 482 ierr = PetscFree(ni->nodeIdx);CHKERRQ(ierr); 483 ni->nodeIdx = newNodeIdx; 484 ni->perm = invClosureOrder; 485 ierr = PetscFree(revlexOrder);CHKERRQ(ierr); 486 ierr = PetscFree(closureOrder);CHKERRQ(ierr); 487 PetscFunctionReturn(0); 488 } 489 490 /* the coordinates of the simplex vertices are the corners of the barycentric simplex. 491 * When we stack them on top of each other in revlex order, they look like the identity matrix */ 492 static PetscErrorCode PetscLagNodeIndicesCreateSimplexVertices(DM dm, PetscLagNodeIndices *nodeIndices) 493 { 494 PetscLagNodeIndices ni; 495 PetscInt dim, d; 496 497 PetscErrorCode ierr; 498 499 PetscFunctionBegin; 500 ierr = PetscNew(&ni);CHKERRQ(ierr); 501 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 502 ni->nodeIdxDim = dim + 1; 503 ni->nodeVecDim = 0; 504 ni->nNodes = dim + 1; 505 ni->refct = 1; 506 ierr = PetscCalloc1((dim + 1)*(dim + 1), &(ni->nodeIdx));CHKERRQ(ierr); 507 for (d = 0; d < dim + 1; d++) ni->nodeIdx[d*(dim + 2)] = 1; 508 ierr = PetscLagNodeIndicesComputeVertexOrder(dm, ni, PETSC_FALSE);CHKERRQ(ierr); 509 *nodeIndices = ni; 510 PetscFunctionReturn(0); 511 } 512 513 /* A polytope that is a tensor product of a facet and a segment. 514 * We take whatever coordinate system was being used for the facet 515 * and we concatenate the barycentric coordinates for the vertices 516 * at the end of the segment, (1,0) and (0,1), to get a coordinate 517 * system for the tensor product element */ 518 static PetscErrorCode PetscLagNodeIndicesCreateTensorVertices(DM dm, PetscLagNodeIndices facetni, PetscLagNodeIndices *nodeIndices) 519 { 520 PetscLagNodeIndices ni; 521 PetscInt nodeIdxDim, subNodeIdxDim = facetni->nodeIdxDim; 522 PetscInt nVerts, nSubVerts = facetni->nNodes; 523 PetscInt dim, d, e, f, g; 524 525 PetscErrorCode ierr; 526 527 PetscFunctionBegin; 528 ierr = PetscNew(&ni);CHKERRQ(ierr); 529 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 530 ni->nodeIdxDim = nodeIdxDim = subNodeIdxDim + 2; 531 ni->nodeVecDim = 0; 532 ni->nNodes = nVerts = 2 * nSubVerts; 533 ni->refct = 1; 534 ierr = PetscCalloc1(nodeIdxDim * nVerts, &(ni->nodeIdx));CHKERRQ(ierr); 535 for (f = 0, d = 0; d < 2; d++) { 536 for (e = 0; e < nSubVerts; e++, f++) { 537 for (g = 0; g < subNodeIdxDim; g++) { 538 ni->nodeIdx[f * nodeIdxDim + g] = facetni->nodeIdx[e * subNodeIdxDim + g]; 539 } 540 ni->nodeIdx[f * nodeIdxDim + subNodeIdxDim] = (1 - d); 541 ni->nodeIdx[f * nodeIdxDim + subNodeIdxDim + 1] = d; 542 } 543 } 544 ierr = PetscLagNodeIndicesComputeVertexOrder(dm, ni, PETSC_TRUE);CHKERRQ(ierr); 545 *nodeIndices = ni; 546 PetscFunctionReturn(0); 547 } 548 549 /* This helps us compute symmetries, and it also helps us compute coordinates for dofs that are being pushed 550 * forward from a boundary mesh point. 551 * 552 * Input: 553 * 554 * dm - the target reference cell where we want new coordinates and dof directions to be valid 555 * vert - the vertex coordinate system for the target reference cell 556 * p - the point in the target reference cell that the dofs are coming from 557 * vertp - the vertex coordinate system for p's reference cell 558 * ornt - the resulting coordinates and dof vectors will be for p under this orientation 559 * nodep - the node coordinates and dof vectors in p's reference cell 560 * formDegree - the form degree that the dofs transform as 561 * 562 * Output: 563 * 564 * pfNodeIdx - the node coordinates for p's dofs, in the dm reference cell, from the ornt perspective 565 * pfNodeVec - the node dof vectors for p's dofs, in the dm reference cell, from the ornt perspective 566 */ 567 static PetscErrorCode PetscLagNodeIndicesPushForward(DM dm, PetscLagNodeIndices vert, PetscInt p, PetscLagNodeIndices vertp, PetscLagNodeIndices nodep, PetscInt ornt, PetscInt formDegree, PetscInt pfNodeIdx[], PetscReal pfNodeVec[]) 568 { 569 PetscInt *closureVerts; 570 PetscInt closureSize = 0; 571 PetscInt *closure = NULL; 572 PetscInt dim, pdim, c, i, j, k, n, v, vStart, vEnd; 573 PetscInt nSubVert = vertp->nNodes; 574 PetscInt nodeIdxDim = vert->nodeIdxDim; 575 PetscInt subNodeIdxDim = vertp->nodeIdxDim; 576 PetscInt nNodes = nodep->nNodes; 577 const PetscInt *vertIdx = vert->nodeIdx; 578 const PetscInt *subVertIdx = vertp->nodeIdx; 579 const PetscInt *nodeIdx = nodep->nodeIdx; 580 const PetscReal *nodeVec = nodep->nodeVec; 581 PetscReal *J, *Jstar; 582 PetscReal detJ; 583 PetscInt depth, pdepth, Nk, pNk; 584 Vec coordVec; 585 PetscScalar *newCoords = NULL; 586 const PetscScalar *oldCoords = NULL; 587 PetscErrorCode ierr; 588 589 PetscFunctionBegin; 590 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 591 ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 592 ierr = DMGetCoordinatesLocal(dm, &coordVec);CHKERRQ(ierr); 593 ierr = DMPlexGetPointDepth(dm, p, &pdepth);CHKERRQ(ierr); 594 pdim = pdepth != depth ? pdepth != 0 ? pdepth : 0 : dim; 595 ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 596 ierr = DMGetWorkArray(dm, nSubVert, MPIU_INT, &closureVerts);CHKERRQ(ierr); 597 ierr = DMPlexGetTransitiveClosure_Internal(dm, p, ornt, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 598 c = closureSize - nSubVert; 599 /* we want which cell closure indices the closure of this point corresponds to */ 600 for (v = 0; v < nSubVert; v++) closureVerts[v] = vert->perm[closure[2 * (c + v)] - vStart]; 601 ierr = DMPlexRestoreTransitiveClosure(dm, p, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 602 /* push forward indices */ 603 for (i = 0; i < nodeIdxDim; i++) { /* for every component of the target index space */ 604 /* check if this is a component that all vertices around this point have in common */ 605 for (j = 1; j < nSubVert; j++) { 606 if (vertIdx[closureVerts[j] * nodeIdxDim + i] != vertIdx[closureVerts[0] * nodeIdxDim + i]) break; 607 } 608 if (j == nSubVert) { /* all vertices have this component in common, directly copy to output */ 609 PetscInt val = vertIdx[closureVerts[0] * nodeIdxDim + i]; 610 for (n = 0; n < nNodes; n++) pfNodeIdx[n * nodeIdxDim + i] = val; 611 } else { 612 PetscInt subi = -1; 613 /* there must be a component in vertp that looks the same */ 614 for (k = 0; k < subNodeIdxDim; k++) { 615 for (j = 0; j < nSubVert; j++) { 616 if (vertIdx[closureVerts[j] * nodeIdxDim + i] != subVertIdx[j * subNodeIdxDim + k]) break; 617 } 618 if (j == nSubVert) { 619 subi = k; 620 break; 621 } 622 } 623 PetscCheckFalse(subi < 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Did not find matching coordinate"); 624 /* that component in the vertp system becomes component i in the vert system for each dof */ 625 for (n = 0; n < nNodes; n++) pfNodeIdx[n * nodeIdxDim + i] = nodeIdx[n * subNodeIdxDim + subi]; 626 } 627 } 628 /* push forward vectors */ 629 ierr = DMGetWorkArray(dm, dim * dim, MPIU_REAL, &J);CHKERRQ(ierr); 630 if (ornt != 0) { /* temporarily change the coordinate vector so 631 DMPlexComputeCellGeometryAffineFEM gives us the Jacobian we want */ 632 PetscInt closureSize2 = 0; 633 PetscInt *closure2 = NULL; 634 635 ierr = DMPlexGetTransitiveClosure_Internal(dm, p, 0, PETSC_TRUE, &closureSize2, &closure2);CHKERRQ(ierr); 636 ierr = PetscMalloc1(dim * nSubVert, &newCoords);CHKERRQ(ierr); 637 ierr = VecGetArrayRead(coordVec, &oldCoords);CHKERRQ(ierr); 638 for (v = 0; v < nSubVert; v++) { 639 PetscInt d; 640 for (d = 0; d < dim; d++) { 641 newCoords[(closure2[2 * (c + v)] - vStart) * dim + d] = oldCoords[closureVerts[v] * dim + d]; 642 } 643 } 644 ierr = VecRestoreArrayRead(coordVec, &oldCoords);CHKERRQ(ierr); 645 ierr = DMPlexRestoreTransitiveClosure(dm, p, PETSC_TRUE, &closureSize2, &closure2);CHKERRQ(ierr); 646 ierr = VecPlaceArray(coordVec, newCoords);CHKERRQ(ierr); 647 } 648 ierr = DMPlexComputeCellGeometryAffineFEM(dm, p, NULL, J, NULL, &detJ);CHKERRQ(ierr); 649 if (ornt != 0) { 650 ierr = VecResetArray(coordVec);CHKERRQ(ierr); 651 ierr = PetscFree(newCoords);CHKERRQ(ierr); 652 } 653 ierr = DMRestoreWorkArray(dm, nSubVert, MPIU_INT, &closureVerts);CHKERRQ(ierr); 654 /* compactify */ 655 for (i = 0; i < dim; i++) for (j = 0; j < pdim; j++) J[i * pdim + j] = J[i * dim + j]; 656 /* We have the Jacobian mapping the point's reference cell to this reference cell: 657 * pulling back a function to the point and applying the dof is what we want, 658 * so we get the pullback matrix and multiply the dof by that matrix on the right */ 659 ierr = PetscDTBinomialInt(dim, PetscAbsInt(formDegree), &Nk);CHKERRQ(ierr); 660 ierr = PetscDTBinomialInt(pdim, PetscAbsInt(formDegree), &pNk);CHKERRQ(ierr); 661 ierr = DMGetWorkArray(dm, pNk * Nk, MPIU_REAL, &Jstar);CHKERRQ(ierr); 662 ierr = PetscDTAltVPullbackMatrix(pdim, dim, J, formDegree, Jstar);CHKERRQ(ierr); 663 for (n = 0; n < nNodes; n++) { 664 for (i = 0; i < Nk; i++) { 665 PetscReal val = 0.; 666 for (j = 0; j < pNk; j++) val += nodeVec[n * pNk + j] * Jstar[j * Nk + i]; 667 pfNodeVec[n * Nk + i] = val; 668 } 669 } 670 ierr = DMRestoreWorkArray(dm, pNk * Nk, MPIU_REAL, &Jstar);CHKERRQ(ierr); 671 ierr = DMRestoreWorkArray(dm, dim * dim, MPIU_REAL, &J);CHKERRQ(ierr); 672 PetscFunctionReturn(0); 673 } 674 675 /* given to sets of nodes, take the tensor product, where the product of the dof indices is concatenation and the 676 * product of the dof vectors is the wedge product */ 677 static PetscErrorCode PetscLagNodeIndicesTensor(PetscLagNodeIndices tracei, PetscInt dimT, PetscInt kT, PetscLagNodeIndices fiberi, PetscInt dimF, PetscInt kF, PetscLagNodeIndices *nodeIndices) 678 { 679 PetscInt dim = dimT + dimF; 680 PetscInt nodeIdxDim, nNodes; 681 PetscInt formDegree = kT + kF; 682 PetscInt Nk, NkT, NkF; 683 PetscInt MkT, MkF; 684 PetscLagNodeIndices ni; 685 PetscInt i, j, l; 686 PetscReal *projF, *projT; 687 PetscReal *projFstar, *projTstar; 688 PetscReal *workF, *workF2, *workT, *workT2, *work, *work2; 689 PetscReal *wedgeMat; 690 PetscReal sign; 691 PetscErrorCode ierr; 692 693 PetscFunctionBegin; 694 ierr = PetscDTBinomialInt(dim, PetscAbsInt(formDegree), &Nk);CHKERRQ(ierr); 695 ierr = PetscDTBinomialInt(dimT, PetscAbsInt(kT), &NkT);CHKERRQ(ierr); 696 ierr = PetscDTBinomialInt(dimF, PetscAbsInt(kF), &NkF);CHKERRQ(ierr); 697 ierr = PetscDTBinomialInt(dim, PetscAbsInt(kT), &MkT);CHKERRQ(ierr); 698 ierr = PetscDTBinomialInt(dim, PetscAbsInt(kF), &MkF);CHKERRQ(ierr); 699 ierr = PetscNew(&ni);CHKERRQ(ierr); 700 ni->nodeIdxDim = nodeIdxDim = tracei->nodeIdxDim + fiberi->nodeIdxDim; 701 ni->nodeVecDim = Nk; 702 ni->nNodes = nNodes = tracei->nNodes * fiberi->nNodes; 703 ni->refct = 1; 704 ierr = PetscMalloc1(nNodes * nodeIdxDim, &(ni->nodeIdx));CHKERRQ(ierr); 705 /* first concatenate the indices */ 706 for (l = 0, j = 0; j < fiberi->nNodes; j++) { 707 for (i = 0; i < tracei->nNodes; i++, l++) { 708 PetscInt m, n = 0; 709 710 for (m = 0; m < tracei->nodeIdxDim; m++) ni->nodeIdx[l * nodeIdxDim + n++] = tracei->nodeIdx[i * tracei->nodeIdxDim + m]; 711 for (m = 0; m < fiberi->nodeIdxDim; m++) ni->nodeIdx[l * nodeIdxDim + n++] = fiberi->nodeIdx[j * fiberi->nodeIdxDim + m]; 712 } 713 } 714 715 /* now wedge together the push-forward vectors */ 716 ierr = PetscMalloc1(nNodes * Nk, &(ni->nodeVec));CHKERRQ(ierr); 717 ierr = PetscCalloc2(dimT*dim, &projT, dimF*dim, &projF);CHKERRQ(ierr); 718 for (i = 0; i < dimT; i++) projT[i * (dim + 1)] = 1.; 719 for (i = 0; i < dimF; i++) projF[i * (dim + dimT + 1) + dimT] = 1.; 720 ierr = PetscMalloc2(MkT*NkT, &projTstar, MkF*NkF, &projFstar);CHKERRQ(ierr); 721 ierr = PetscDTAltVPullbackMatrix(dim, dimT, projT, kT, projTstar);CHKERRQ(ierr); 722 ierr = PetscDTAltVPullbackMatrix(dim, dimF, projF, kF, projFstar);CHKERRQ(ierr); 723 ierr = PetscMalloc6(MkT, &workT, MkT, &workT2, MkF, &workF, MkF, &workF2, Nk, &work, Nk, &work2);CHKERRQ(ierr); 724 ierr = PetscMalloc1(Nk * MkT, &wedgeMat);CHKERRQ(ierr); 725 sign = (PetscAbsInt(kT * kF) & 1) ? -1. : 1.; 726 for (l = 0, j = 0; j < fiberi->nNodes; j++) { 727 PetscInt d, e; 728 729 /* push forward fiber k-form */ 730 for (d = 0; d < MkF; d++) { 731 PetscReal val = 0.; 732 for (e = 0; e < NkF; e++) val += projFstar[d * NkF + e] * fiberi->nodeVec[j * NkF + e]; 733 workF[d] = val; 734 } 735 /* Hodge star to proper form if necessary */ 736 if (kF < 0) { 737 for (d = 0; d < MkF; d++) workF2[d] = workF[d]; 738 ierr = PetscDTAltVStar(dim, PetscAbsInt(kF), 1, workF2, workF);CHKERRQ(ierr); 739 } 740 /* Compute the matrix that wedges this form with one of the trace k-form */ 741 ierr = PetscDTAltVWedgeMatrix(dim, PetscAbsInt(kF), PetscAbsInt(kT), workF, wedgeMat);CHKERRQ(ierr); 742 for (i = 0; i < tracei->nNodes; i++, l++) { 743 /* push forward trace k-form */ 744 for (d = 0; d < MkT; d++) { 745 PetscReal val = 0.; 746 for (e = 0; e < NkT; e++) val += projTstar[d * NkT + e] * tracei->nodeVec[i * NkT + e]; 747 workT[d] = val; 748 } 749 /* Hodge star to proper form if necessary */ 750 if (kT < 0) { 751 for (d = 0; d < MkT; d++) workT2[d] = workT[d]; 752 ierr = PetscDTAltVStar(dim, PetscAbsInt(kT), 1, workT2, workT);CHKERRQ(ierr); 753 } 754 /* compute the wedge product of the push-forward trace form and firer forms */ 755 for (d = 0; d < Nk; d++) { 756 PetscReal val = 0.; 757 for (e = 0; e < MkT; e++) val += wedgeMat[d * MkT + e] * workT[e]; 758 work[d] = val; 759 } 760 /* inverse Hodge star from proper form if necessary */ 761 if (formDegree < 0) { 762 for (d = 0; d < Nk; d++) work2[d] = work[d]; 763 ierr = PetscDTAltVStar(dim, PetscAbsInt(formDegree), -1, work2, work);CHKERRQ(ierr); 764 } 765 /* insert into the array (adjusting for sign) */ 766 for (d = 0; d < Nk; d++) ni->nodeVec[l * Nk + d] = sign * work[d]; 767 } 768 } 769 ierr = PetscFree(wedgeMat);CHKERRQ(ierr); 770 ierr = PetscFree6(workT, workT2, workF, workF2, work, work2);CHKERRQ(ierr); 771 ierr = PetscFree2(projTstar, projFstar);CHKERRQ(ierr); 772 ierr = PetscFree2(projT, projF);CHKERRQ(ierr); 773 *nodeIndices = ni; 774 PetscFunctionReturn(0); 775 } 776 777 /* simple union of two sets of nodes */ 778 static PetscErrorCode PetscLagNodeIndicesMerge(PetscLagNodeIndices niA, PetscLagNodeIndices niB, PetscLagNodeIndices *nodeIndices) 779 { 780 PetscLagNodeIndices ni; 781 PetscInt nodeIdxDim, nodeVecDim, nNodes; 782 PetscErrorCode ierr; 783 784 PetscFunctionBegin; 785 ierr = PetscNew(&ni);CHKERRQ(ierr); 786 ni->nodeIdxDim = nodeIdxDim = niA->nodeIdxDim; 787 PetscCheckFalse(niB->nodeIdxDim != nodeIdxDim,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Cannot merge PetscLagNodeIndices with different nodeIdxDim"); 788 ni->nodeVecDim = nodeVecDim = niA->nodeVecDim; 789 PetscCheckFalse(niB->nodeVecDim != nodeVecDim,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Cannot merge PetscLagNodeIndices with different nodeVecDim"); 790 ni->nNodes = nNodes = niA->nNodes + niB->nNodes; 791 ni->refct = 1; 792 ierr = PetscMalloc1(nNodes * nodeIdxDim, &(ni->nodeIdx));CHKERRQ(ierr); 793 ierr = PetscMalloc1(nNodes * nodeVecDim, &(ni->nodeVec));CHKERRQ(ierr); 794 ierr = PetscArraycpy(ni->nodeIdx, niA->nodeIdx, niA->nNodes * nodeIdxDim);CHKERRQ(ierr); 795 ierr = PetscArraycpy(ni->nodeVec, niA->nodeVec, niA->nNodes * nodeVecDim);CHKERRQ(ierr); 796 ierr = PetscArraycpy(&(ni->nodeIdx[niA->nNodes * nodeIdxDim]), niB->nodeIdx, niB->nNodes * nodeIdxDim);CHKERRQ(ierr); 797 ierr = PetscArraycpy(&(ni->nodeVec[niA->nNodes * nodeVecDim]), niB->nodeVec, niB->nNodes * nodeVecDim);CHKERRQ(ierr); 798 *nodeIndices = ni; 799 PetscFunctionReturn(0); 800 } 801 802 #define PETSCTUPINTCOMPREVLEX(N) \ 803 static int PetscConcat_(PetscTupIntCompRevlex_,N)(const void *a, const void *b) \ 804 { \ 805 const PetscInt *A = (const PetscInt *) a; \ 806 const PetscInt *B = (const PetscInt *) b; \ 807 int i; \ 808 PetscInt diff = 0; \ 809 for (i = 0; i < N; i++) { \ 810 diff = A[N - i] - B[N - i]; \ 811 if (diff) break; \ 812 } \ 813 return (diff <= 0) ? (diff < 0) ? -1 : 0 : 1; \ 814 } 815 816 PETSCTUPINTCOMPREVLEX(3) 817 PETSCTUPINTCOMPREVLEX(4) 818 PETSCTUPINTCOMPREVLEX(5) 819 PETSCTUPINTCOMPREVLEX(6) 820 PETSCTUPINTCOMPREVLEX(7) 821 822 static int PetscTupIntCompRevlex_N(const void *a, const void *b) 823 { 824 const PetscInt *A = (const PetscInt *) a; 825 const PetscInt *B = (const PetscInt *) b; 826 int i; 827 int N = A[0]; 828 PetscInt diff = 0; 829 for (i = 0; i < N; i++) { 830 diff = A[N - i] - B[N - i]; 831 if (diff) break; 832 } 833 return (diff <= 0) ? (diff < 0) ? -1 : 0 : 1; 834 } 835 836 /* The nodes are not necessarily in revlex order wrt nodeIdx: get the permutation 837 * that puts them in that order */ 838 static PetscErrorCode PetscLagNodeIndicesGetPermutation(PetscLagNodeIndices ni, PetscInt *perm[]) 839 { 840 PetscErrorCode ierr; 841 842 PetscFunctionBegin; 843 if (!(ni->perm)) { 844 PetscInt *sorter; 845 PetscInt m = ni->nNodes; 846 PetscInt nodeIdxDim = ni->nodeIdxDim; 847 PetscInt i, j, k, l; 848 PetscInt *prm; 849 int (*comp) (const void *, const void *); 850 851 ierr = PetscMalloc1((nodeIdxDim + 2) * m, &sorter);CHKERRQ(ierr); 852 for (k = 0, l = 0, i = 0; i < m; i++) { 853 sorter[k++] = nodeIdxDim + 1; 854 sorter[k++] = i; 855 for (j = 0; j < nodeIdxDim; j++) sorter[k++] = ni->nodeIdx[l++]; 856 } 857 switch (nodeIdxDim) { 858 case 2: 859 comp = PetscTupIntCompRevlex_3; 860 break; 861 case 3: 862 comp = PetscTupIntCompRevlex_4; 863 break; 864 case 4: 865 comp = PetscTupIntCompRevlex_5; 866 break; 867 case 5: 868 comp = PetscTupIntCompRevlex_6; 869 break; 870 case 6: 871 comp = PetscTupIntCompRevlex_7; 872 break; 873 default: 874 comp = PetscTupIntCompRevlex_N; 875 break; 876 } 877 qsort(sorter, m, (nodeIdxDim + 2) * sizeof(PetscInt), comp); 878 ierr = PetscMalloc1(m, &prm);CHKERRQ(ierr); 879 for (i = 0; i < m; i++) prm[i] = sorter[(nodeIdxDim + 2) * i + 1]; 880 ni->perm = prm; 881 ierr = PetscFree(sorter);CHKERRQ(ierr); 882 } 883 *perm = ni->perm; 884 PetscFunctionReturn(0); 885 } 886 887 static PetscErrorCode PetscDualSpaceDestroy_Lagrange(PetscDualSpace sp) 888 { 889 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *) sp->data; 890 PetscErrorCode ierr; 891 892 PetscFunctionBegin; 893 if (lag->symperms) { 894 PetscInt **selfSyms = lag->symperms[0]; 895 896 if (selfSyms) { 897 PetscInt i, **allocated = &selfSyms[-lag->selfSymOff]; 898 899 for (i = 0; i < lag->numSelfSym; i++) { 900 ierr = PetscFree(allocated[i]);CHKERRQ(ierr); 901 } 902 ierr = PetscFree(allocated);CHKERRQ(ierr); 903 } 904 ierr = PetscFree(lag->symperms);CHKERRQ(ierr); 905 } 906 if (lag->symflips) { 907 PetscScalar **selfSyms = lag->symflips[0]; 908 909 if (selfSyms) { 910 PetscInt i; 911 PetscScalar **allocated = &selfSyms[-lag->selfSymOff]; 912 913 for (i = 0; i < lag->numSelfSym; i++) { 914 ierr = PetscFree(allocated[i]);CHKERRQ(ierr); 915 } 916 ierr = PetscFree(allocated);CHKERRQ(ierr); 917 } 918 ierr = PetscFree(lag->symflips);CHKERRQ(ierr); 919 } 920 ierr = Petsc1DNodeFamilyDestroy(&(lag->nodeFamily));CHKERRQ(ierr); 921 ierr = PetscLagNodeIndicesDestroy(&(lag->vertIndices));CHKERRQ(ierr); 922 ierr = PetscLagNodeIndicesDestroy(&(lag->intNodeIndices));CHKERRQ(ierr); 923 ierr = PetscLagNodeIndicesDestroy(&(lag->allNodeIndices));CHKERRQ(ierr); 924 ierr = PetscFree(lag);CHKERRQ(ierr); 925 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetContinuity_C", NULL);CHKERRQ(ierr); 926 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetContinuity_C", NULL);CHKERRQ(ierr); 927 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetTensor_C", NULL);CHKERRQ(ierr); 928 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetTensor_C", NULL);CHKERRQ(ierr); 929 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetTrimmed_C", NULL);CHKERRQ(ierr); 930 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetTrimmed_C", NULL);CHKERRQ(ierr); 931 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetNodeType_C", NULL);CHKERRQ(ierr); 932 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetNodeType_C", NULL);CHKERRQ(ierr); 933 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetUseMoments_C", NULL);CHKERRQ(ierr); 934 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetUseMoments_C", NULL);CHKERRQ(ierr); 935 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetMomentOrder_C", NULL);CHKERRQ(ierr); 936 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetMomentOrder_C", NULL);CHKERRQ(ierr); 937 PetscFunctionReturn(0); 938 } 939 940 static PetscErrorCode PetscDualSpaceLagrangeView_Ascii(PetscDualSpace sp, PetscViewer viewer) 941 { 942 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *) sp->data; 943 PetscErrorCode ierr; 944 945 PetscFunctionBegin; 946 ierr = PetscViewerASCIIPrintf(viewer, "%s %s%sLagrange dual space\n", lag->continuous ? "Continuous" : "Discontinuous", lag->tensorSpace ? "tensor " : "", lag->trimmed ? "trimmed " : "");CHKERRQ(ierr); 947 PetscFunctionReturn(0); 948 } 949 950 static PetscErrorCode PetscDualSpaceView_Lagrange(PetscDualSpace sp, PetscViewer viewer) 951 { 952 PetscBool iascii; 953 PetscErrorCode ierr; 954 955 PetscFunctionBegin; 956 PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1); 957 PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 958 ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 959 if (iascii) {ierr = PetscDualSpaceLagrangeView_Ascii(sp, viewer);CHKERRQ(ierr);} 960 PetscFunctionReturn(0); 961 } 962 963 static PetscErrorCode PetscDualSpaceSetFromOptions_Lagrange(PetscOptionItems *PetscOptionsObject,PetscDualSpace sp) 964 { 965 PetscBool continuous, tensor, trimmed, flg, flg2, flg3; 966 PetscDTNodeType nodeType; 967 PetscReal nodeExponent; 968 PetscInt momentOrder; 969 PetscBool nodeEndpoints, useMoments; 970 PetscErrorCode ierr; 971 972 PetscFunctionBegin; 973 ierr = PetscDualSpaceLagrangeGetContinuity(sp, &continuous);CHKERRQ(ierr); 974 ierr = PetscDualSpaceLagrangeGetTensor(sp, &tensor);CHKERRQ(ierr); 975 ierr = PetscDualSpaceLagrangeGetTrimmed(sp, &trimmed);CHKERRQ(ierr); 976 ierr = PetscDualSpaceLagrangeGetNodeType(sp, &nodeType, &nodeEndpoints, &nodeExponent);CHKERRQ(ierr); 977 if (nodeType == PETSCDTNODES_DEFAULT) nodeType = PETSCDTNODES_GAUSSJACOBI; 978 ierr = PetscDualSpaceLagrangeGetUseMoments(sp, &useMoments);CHKERRQ(ierr); 979 ierr = PetscDualSpaceLagrangeGetMomentOrder(sp, &momentOrder);CHKERRQ(ierr); 980 ierr = PetscOptionsHead(PetscOptionsObject,"PetscDualSpace Lagrange Options");CHKERRQ(ierr); 981 ierr = PetscOptionsBool("-petscdualspace_lagrange_continuity", "Flag for continuous element", "PetscDualSpaceLagrangeSetContinuity", continuous, &continuous, &flg);CHKERRQ(ierr); 982 if (flg) {ierr = PetscDualSpaceLagrangeSetContinuity(sp, continuous);CHKERRQ(ierr);} 983 ierr = PetscOptionsBool("-petscdualspace_lagrange_tensor", "Flag for tensor dual space", "PetscDualSpaceLagrangeSetTensor", tensor, &tensor, &flg);CHKERRQ(ierr); 984 if (flg) {ierr = PetscDualSpaceLagrangeSetTensor(sp, tensor);CHKERRQ(ierr);} 985 ierr = PetscOptionsBool("-petscdualspace_lagrange_trimmed", "Flag for trimmed dual space", "PetscDualSpaceLagrangeSetTrimmed", trimmed, &trimmed, &flg);CHKERRQ(ierr); 986 if (flg) {ierr = PetscDualSpaceLagrangeSetTrimmed(sp, trimmed);CHKERRQ(ierr);} 987 ierr = PetscOptionsEnum("-petscdualspace_lagrange_node_type", "Lagrange node location type", "PetscDualSpaceLagrangeSetNodeType", PetscDTNodeTypes, (PetscEnum)nodeType, (PetscEnum *)&nodeType, &flg);CHKERRQ(ierr); 988 ierr = PetscOptionsBool("-petscdualspace_lagrange_node_endpoints", "Flag for nodes that include endpoints", "PetscDualSpaceLagrangeSetNodeType", nodeEndpoints, &nodeEndpoints, &flg2);CHKERRQ(ierr); 989 flg3 = PETSC_FALSE; 990 if (nodeType == PETSCDTNODES_GAUSSJACOBI) { 991 ierr = PetscOptionsReal("-petscdualspace_lagrange_node_exponent", "Gauss-Jacobi weight function exponent", "PetscDualSpaceLagrangeSetNodeType", nodeExponent, &nodeExponent, &flg3);CHKERRQ(ierr); 992 } 993 if (flg || flg2 || flg3) {ierr = PetscDualSpaceLagrangeSetNodeType(sp, nodeType, nodeEndpoints, nodeExponent);CHKERRQ(ierr);} 994 ierr = PetscOptionsBool("-petscdualspace_lagrange_use_moments", "Use moments (where appropriate) for functionals", "PetscDualSpaceLagrangeSetUseMoments", useMoments, &useMoments, &flg);CHKERRQ(ierr); 995 if (flg) {ierr = PetscDualSpaceLagrangeSetUseMoments(sp, useMoments);CHKERRQ(ierr);} 996 ierr = PetscOptionsInt("-petscdualspace_lagrange_moment_order", "Quadrature order for moment functionals", "PetscDualSpaceLagrangeSetMomentOrder", momentOrder, &momentOrder, &flg);CHKERRQ(ierr); 997 if (flg) {ierr = PetscDualSpaceLagrangeSetMomentOrder(sp, momentOrder);CHKERRQ(ierr);} 998 ierr = PetscOptionsTail();CHKERRQ(ierr); 999 PetscFunctionReturn(0); 1000 } 1001 1002 static PetscErrorCode PetscDualSpaceDuplicate_Lagrange(PetscDualSpace sp, PetscDualSpace spNew) 1003 { 1004 PetscBool cont, tensor, trimmed, boundary; 1005 PetscDTNodeType nodeType; 1006 PetscReal exponent; 1007 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *) sp->data; 1008 PetscErrorCode ierr; 1009 1010 PetscFunctionBegin; 1011 ierr = PetscDualSpaceLagrangeGetContinuity(sp, &cont);CHKERRQ(ierr); 1012 ierr = PetscDualSpaceLagrangeSetContinuity(spNew, cont);CHKERRQ(ierr); 1013 ierr = PetscDualSpaceLagrangeGetTensor(sp, &tensor);CHKERRQ(ierr); 1014 ierr = PetscDualSpaceLagrangeSetTensor(spNew, tensor);CHKERRQ(ierr); 1015 ierr = PetscDualSpaceLagrangeGetTrimmed(sp, &trimmed);CHKERRQ(ierr); 1016 ierr = PetscDualSpaceLagrangeSetTrimmed(spNew, trimmed);CHKERRQ(ierr); 1017 ierr = PetscDualSpaceLagrangeGetNodeType(sp, &nodeType, &boundary, &exponent);CHKERRQ(ierr); 1018 ierr = PetscDualSpaceLagrangeSetNodeType(spNew, nodeType, boundary, exponent);CHKERRQ(ierr); 1019 if (lag->nodeFamily) { 1020 PetscDualSpace_Lag *lagnew = (PetscDualSpace_Lag *) spNew->data; 1021 1022 ierr = Petsc1DNodeFamilyReference(lag->nodeFamily);CHKERRQ(ierr); 1023 lagnew->nodeFamily = lag->nodeFamily; 1024 } 1025 PetscFunctionReturn(0); 1026 } 1027 1028 /* for making tensor product spaces: take a dual space and product a segment space that has all the same 1029 * specifications (trimmed, continuous, order, node set), except for the form degree */ 1030 static PetscErrorCode PetscDualSpaceCreateEdgeSubspace_Lagrange(PetscDualSpace sp, PetscInt order, PetscInt k, PetscInt Nc, PetscBool interiorOnly, PetscDualSpace *bdsp) 1031 { 1032 DM K; 1033 PetscDualSpace_Lag *newlag; 1034 PetscErrorCode ierr; 1035 1036 PetscFunctionBegin; 1037 ierr = PetscDualSpaceDuplicate(sp,bdsp);CHKERRQ(ierr); 1038 ierr = PetscDualSpaceSetFormDegree(*bdsp, k);CHKERRQ(ierr); 1039 ierr = DMPlexCreateReferenceCell(PETSC_COMM_SELF, DMPolytopeTypeSimpleShape(1, PETSC_TRUE), &K);CHKERRQ(ierr); 1040 ierr = PetscDualSpaceSetDM(*bdsp, K);CHKERRQ(ierr); 1041 ierr = DMDestroy(&K);CHKERRQ(ierr); 1042 ierr = PetscDualSpaceSetOrder(*bdsp, order);CHKERRQ(ierr); 1043 ierr = PetscDualSpaceSetNumComponents(*bdsp, Nc);CHKERRQ(ierr); 1044 newlag = (PetscDualSpace_Lag *) (*bdsp)->data; 1045 newlag->interiorOnly = interiorOnly; 1046 ierr = PetscDualSpaceSetUp(*bdsp);CHKERRQ(ierr); 1047 PetscFunctionReturn(0); 1048 } 1049 1050 /* just the points, weights aren't handled */ 1051 static PetscErrorCode PetscQuadratureCreateTensor(PetscQuadrature trace, PetscQuadrature fiber, PetscQuadrature *product) 1052 { 1053 PetscInt dimTrace, dimFiber; 1054 PetscInt numPointsTrace, numPointsFiber; 1055 PetscInt dim, numPoints; 1056 const PetscReal *pointsTrace; 1057 const PetscReal *pointsFiber; 1058 PetscReal *points; 1059 PetscInt i, j, k, p; 1060 PetscErrorCode ierr; 1061 1062 PetscFunctionBegin; 1063 ierr = PetscQuadratureGetData(trace, &dimTrace, NULL, &numPointsTrace, &pointsTrace, NULL);CHKERRQ(ierr); 1064 ierr = PetscQuadratureGetData(fiber, &dimFiber, NULL, &numPointsFiber, &pointsFiber, NULL);CHKERRQ(ierr); 1065 dim = dimTrace + dimFiber; 1066 numPoints = numPointsFiber * numPointsTrace; 1067 ierr = PetscMalloc1(numPoints * dim, &points);CHKERRQ(ierr); 1068 for (p = 0, j = 0; j < numPointsFiber; j++) { 1069 for (i = 0; i < numPointsTrace; i++, p++) { 1070 for (k = 0; k < dimTrace; k++) points[p * dim + k] = pointsTrace[i * dimTrace + k]; 1071 for (k = 0; k < dimFiber; k++) points[p * dim + dimTrace + k] = pointsFiber[j * dimFiber + k]; 1072 } 1073 } 1074 ierr = PetscQuadratureCreate(PETSC_COMM_SELF, product);CHKERRQ(ierr); 1075 ierr = PetscQuadratureSetData(*product, dim, 0, numPoints, points, NULL);CHKERRQ(ierr); 1076 PetscFunctionReturn(0); 1077 } 1078 1079 /* Kronecker tensor product where matrix is considered a matrix of k-forms, so that 1080 * the entries in the product matrix are wedge products of the entries in the original matrices */ 1081 static PetscErrorCode MatTensorAltV(Mat trace, Mat fiber, PetscInt dimTrace, PetscInt kTrace, PetscInt dimFiber, PetscInt kFiber, Mat *product) 1082 { 1083 PetscInt mTrace, nTrace, mFiber, nFiber, m, n, k, i, j, l; 1084 PetscInt dim, NkTrace, NkFiber, Nk; 1085 PetscInt dT, dF; 1086 PetscInt *nnzTrace, *nnzFiber, *nnz; 1087 PetscInt iT, iF, jT, jF, il, jl; 1088 PetscReal *workT, *workT2, *workF, *workF2, *work, *workstar; 1089 PetscReal *projT, *projF; 1090 PetscReal *projTstar, *projFstar; 1091 PetscReal *wedgeMat; 1092 PetscReal sign; 1093 PetscScalar *workS; 1094 Mat prod; 1095 /* this produces dof groups that look like the identity */ 1096 PetscErrorCode ierr; 1097 1098 PetscFunctionBegin; 1099 ierr = MatGetSize(trace, &mTrace, &nTrace);CHKERRQ(ierr); 1100 ierr = PetscDTBinomialInt(dimTrace, PetscAbsInt(kTrace), &NkTrace);CHKERRQ(ierr); 1101 PetscCheckFalse(nTrace % NkTrace,PETSC_COMM_SELF, PETSC_ERR_PLIB, "point value space of trace matrix is not a multiple of k-form size"); 1102 ierr = MatGetSize(fiber, &mFiber, &nFiber);CHKERRQ(ierr); 1103 ierr = PetscDTBinomialInt(dimFiber, PetscAbsInt(kFiber), &NkFiber);CHKERRQ(ierr); 1104 PetscCheckFalse(nFiber % NkFiber,PETSC_COMM_SELF, PETSC_ERR_PLIB, "point value space of fiber matrix is not a multiple of k-form size"); 1105 ierr = PetscMalloc2(mTrace, &nnzTrace, mFiber, &nnzFiber);CHKERRQ(ierr); 1106 for (i = 0; i < mTrace; i++) { 1107 ierr = MatGetRow(trace, i, &(nnzTrace[i]), NULL, NULL);CHKERRQ(ierr); 1108 PetscCheckFalse(nnzTrace[i] % NkTrace,PETSC_COMM_SELF, PETSC_ERR_PLIB, "nonzeros in trace matrix are not in k-form size blocks"); 1109 } 1110 for (i = 0; i < mFiber; i++) { 1111 ierr = MatGetRow(fiber, i, &(nnzFiber[i]), NULL, NULL);CHKERRQ(ierr); 1112 PetscCheckFalse(nnzFiber[i] % NkFiber,PETSC_COMM_SELF, PETSC_ERR_PLIB, "nonzeros in fiber matrix are not in k-form size blocks"); 1113 } 1114 dim = dimTrace + dimFiber; 1115 k = kFiber + kTrace; 1116 ierr = PetscDTBinomialInt(dim, PetscAbsInt(k), &Nk);CHKERRQ(ierr); 1117 m = mTrace * mFiber; 1118 ierr = PetscMalloc1(m, &nnz);CHKERRQ(ierr); 1119 for (l = 0, j = 0; j < mFiber; j++) for (i = 0; i < mTrace; i++, l++) nnz[l] = (nnzTrace[i] / NkTrace) * (nnzFiber[j] / NkFiber) * Nk; 1120 n = (nTrace / NkTrace) * (nFiber / NkFiber) * Nk; 1121 ierr = MatCreateSeqAIJ(PETSC_COMM_SELF, m, n, 0, nnz, &prod);CHKERRQ(ierr); 1122 ierr = PetscFree(nnz);CHKERRQ(ierr); 1123 ierr = PetscFree2(nnzTrace,nnzFiber);CHKERRQ(ierr); 1124 /* reasoning about which points each dof needs depends on having zeros computed at points preserved */ 1125 ierr = MatSetOption(prod, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr); 1126 /* compute pullbacks */ 1127 ierr = PetscDTBinomialInt(dim, PetscAbsInt(kTrace), &dT);CHKERRQ(ierr); 1128 ierr = PetscDTBinomialInt(dim, PetscAbsInt(kFiber), &dF);CHKERRQ(ierr); 1129 ierr = PetscMalloc4(dimTrace * dim, &projT, dimFiber * dim, &projF, dT * NkTrace, &projTstar, dF * NkFiber, &projFstar);CHKERRQ(ierr); 1130 ierr = PetscArrayzero(projT, dimTrace * dim);CHKERRQ(ierr); 1131 for (i = 0; i < dimTrace; i++) projT[i * (dim + 1)] = 1.; 1132 ierr = PetscArrayzero(projF, dimFiber * dim);CHKERRQ(ierr); 1133 for (i = 0; i < dimFiber; i++) projF[i * (dim + 1) + dimTrace] = 1.; 1134 ierr = PetscDTAltVPullbackMatrix(dim, dimTrace, projT, kTrace, projTstar);CHKERRQ(ierr); 1135 ierr = PetscDTAltVPullbackMatrix(dim, dimFiber, projF, kFiber, projFstar);CHKERRQ(ierr); 1136 ierr = PetscMalloc5(dT, &workT, dF, &workF, Nk, &work, Nk, &workstar, Nk, &workS);CHKERRQ(ierr); 1137 ierr = PetscMalloc2(dT, &workT2, dF, &workF2);CHKERRQ(ierr); 1138 ierr = PetscMalloc1(Nk * dT, &wedgeMat);CHKERRQ(ierr); 1139 sign = (PetscAbsInt(kTrace * kFiber) & 1) ? -1. : 1.; 1140 for (i = 0, iF = 0; iF < mFiber; iF++) { 1141 PetscInt ncolsF, nformsF; 1142 const PetscInt *colsF; 1143 const PetscScalar *valsF; 1144 1145 ierr = MatGetRow(fiber, iF, &ncolsF, &colsF, &valsF);CHKERRQ(ierr); 1146 nformsF = ncolsF / NkFiber; 1147 for (iT = 0; iT < mTrace; iT++, i++) { 1148 PetscInt ncolsT, nformsT; 1149 const PetscInt *colsT; 1150 const PetscScalar *valsT; 1151 1152 ierr = MatGetRow(trace, iT, &ncolsT, &colsT, &valsT);CHKERRQ(ierr); 1153 nformsT = ncolsT / NkTrace; 1154 for (j = 0, jF = 0; jF < nformsF; jF++) { 1155 PetscInt colF = colsF[jF * NkFiber] / NkFiber; 1156 1157 for (il = 0; il < dF; il++) { 1158 PetscReal val = 0.; 1159 for (jl = 0; jl < NkFiber; jl++) val += projFstar[il * NkFiber + jl] * PetscRealPart(valsF[jF * NkFiber + jl]); 1160 workF[il] = val; 1161 } 1162 if (kFiber < 0) { 1163 for (il = 0; il < dF; il++) workF2[il] = workF[il]; 1164 ierr = PetscDTAltVStar(dim, PetscAbsInt(kFiber), 1, workF2, workF);CHKERRQ(ierr); 1165 } 1166 ierr = PetscDTAltVWedgeMatrix(dim, PetscAbsInt(kFiber), PetscAbsInt(kTrace), workF, wedgeMat);CHKERRQ(ierr); 1167 for (jT = 0; jT < nformsT; jT++, j++) { 1168 PetscInt colT = colsT[jT * NkTrace] / NkTrace; 1169 PetscInt col = colF * (nTrace / NkTrace) + colT; 1170 const PetscScalar *vals; 1171 1172 for (il = 0; il < dT; il++) { 1173 PetscReal val = 0.; 1174 for (jl = 0; jl < NkTrace; jl++) val += projTstar[il * NkTrace + jl] * PetscRealPart(valsT[jT * NkTrace + jl]); 1175 workT[il] = val; 1176 } 1177 if (kTrace < 0) { 1178 for (il = 0; il < dT; il++) workT2[il] = workT[il]; 1179 ierr = PetscDTAltVStar(dim, PetscAbsInt(kTrace), 1, workT2, workT);CHKERRQ(ierr); 1180 } 1181 1182 for (il = 0; il < Nk; il++) { 1183 PetscReal val = 0.; 1184 for (jl = 0; jl < dT; jl++) val += sign * wedgeMat[il * dT + jl] * workT[jl]; 1185 work[il] = val; 1186 } 1187 if (k < 0) { 1188 ierr = PetscDTAltVStar(dim, PetscAbsInt(k), -1, work, workstar);CHKERRQ(ierr); 1189 #if defined(PETSC_USE_COMPLEX) 1190 for (l = 0; l < Nk; l++) workS[l] = workstar[l]; 1191 vals = &workS[0]; 1192 #else 1193 vals = &workstar[0]; 1194 #endif 1195 } else { 1196 #if defined(PETSC_USE_COMPLEX) 1197 for (l = 0; l < Nk; l++) workS[l] = work[l]; 1198 vals = &workS[0]; 1199 #else 1200 vals = &work[0]; 1201 #endif 1202 } 1203 for (l = 0; l < Nk; l++) { 1204 ierr = MatSetValue(prod, i, col * Nk + l, vals[l], INSERT_VALUES);CHKERRQ(ierr); 1205 } /* Nk */ 1206 } /* jT */ 1207 } /* jF */ 1208 ierr = MatRestoreRow(trace, iT, &ncolsT, &colsT, &valsT);CHKERRQ(ierr); 1209 } /* iT */ 1210 ierr = MatRestoreRow(fiber, iF, &ncolsF, &colsF, &valsF);CHKERRQ(ierr); 1211 } /* iF */ 1212 ierr = PetscFree(wedgeMat);CHKERRQ(ierr); 1213 ierr = PetscFree4(projT, projF, projTstar, projFstar);CHKERRQ(ierr); 1214 ierr = PetscFree2(workT2, workF2);CHKERRQ(ierr); 1215 ierr = PetscFree5(workT, workF, work, workstar, workS);CHKERRQ(ierr); 1216 ierr = MatAssemblyBegin(prod, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1217 ierr = MatAssemblyEnd(prod, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1218 *product = prod; 1219 PetscFunctionReturn(0); 1220 } 1221 1222 /* Union of quadrature points, with an attempt to identify commont points in the two sets */ 1223 static PetscErrorCode PetscQuadraturePointsMerge(PetscQuadrature quadA, PetscQuadrature quadB, PetscQuadrature *quadJoint, PetscInt *aToJoint[], PetscInt *bToJoint[]) 1224 { 1225 PetscInt dimA, dimB; 1226 PetscInt nA, nB, nJoint, i, j, d; 1227 const PetscReal *pointsA; 1228 const PetscReal *pointsB; 1229 PetscReal *pointsJoint; 1230 PetscInt *aToJ, *bToJ; 1231 PetscQuadrature qJ; 1232 PetscErrorCode ierr; 1233 1234 PetscFunctionBegin; 1235 ierr = PetscQuadratureGetData(quadA, &dimA, NULL, &nA, &pointsA, NULL);CHKERRQ(ierr); 1236 ierr = PetscQuadratureGetData(quadB, &dimB, NULL, &nB, &pointsB, NULL);CHKERRQ(ierr); 1237 PetscCheckFalse(dimA != dimB,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Quadrature points must be in the same dimension"); 1238 nJoint = nA; 1239 ierr = PetscMalloc1(nA, &aToJ);CHKERRQ(ierr); 1240 for (i = 0; i < nA; i++) aToJ[i] = i; 1241 ierr = PetscMalloc1(nB, &bToJ);CHKERRQ(ierr); 1242 for (i = 0; i < nB; i++) { 1243 for (j = 0; j < nA; j++) { 1244 bToJ[i] = -1; 1245 for (d = 0; d < dimA; d++) if (PetscAbsReal(pointsB[i * dimA + d] - pointsA[j * dimA + d]) > PETSC_SMALL) break; 1246 if (d == dimA) { 1247 bToJ[i] = j; 1248 break; 1249 } 1250 } 1251 if (bToJ[i] == -1) { 1252 bToJ[i] = nJoint++; 1253 } 1254 } 1255 *aToJoint = aToJ; 1256 *bToJoint = bToJ; 1257 ierr = PetscMalloc1(nJoint * dimA, &pointsJoint);CHKERRQ(ierr); 1258 ierr = PetscArraycpy(pointsJoint, pointsA, nA * dimA);CHKERRQ(ierr); 1259 for (i = 0; i < nB; i++) { 1260 if (bToJ[i] >= nA) { 1261 for (d = 0; d < dimA; d++) pointsJoint[bToJ[i] * dimA + d] = pointsB[i * dimA + d]; 1262 } 1263 } 1264 ierr = PetscQuadratureCreate(PETSC_COMM_SELF, &qJ);CHKERRQ(ierr); 1265 ierr = PetscQuadratureSetData(qJ, dimA, 0, nJoint, pointsJoint, NULL);CHKERRQ(ierr); 1266 *quadJoint = qJ; 1267 PetscFunctionReturn(0); 1268 } 1269 1270 /* Matrices matA and matB are both quadrature -> dof matrices: produce a matrix that is joint quadrature -> union of 1271 * dofs, where the joint quadrature was produced by PetscQuadraturePointsMerge */ 1272 static PetscErrorCode MatricesMerge(Mat matA, Mat matB, PetscInt dim, PetscInt k, PetscInt numMerged, const PetscInt aToMerged[], const PetscInt bToMerged[], Mat *matMerged) 1273 { 1274 PetscInt m, n, mA, nA, mB, nB, Nk, i, j, l; 1275 Mat M; 1276 PetscInt *nnz; 1277 PetscInt maxnnz; 1278 PetscInt *work; 1279 PetscErrorCode ierr; 1280 1281 PetscFunctionBegin; 1282 ierr = PetscDTBinomialInt(dim, PetscAbsInt(k), &Nk);CHKERRQ(ierr); 1283 ierr = MatGetSize(matA, &mA, &nA);CHKERRQ(ierr); 1284 PetscCheckFalse(nA % Nk,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "matA column space not a multiple of k-form size"); 1285 ierr = MatGetSize(matB, &mB, &nB);CHKERRQ(ierr); 1286 PetscCheckFalse(nB % Nk,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "matB column space not a multiple of k-form size"); 1287 m = mA + mB; 1288 n = numMerged * Nk; 1289 ierr = PetscMalloc1(m, &nnz);CHKERRQ(ierr); 1290 maxnnz = 0; 1291 for (i = 0; i < mA; i++) { 1292 ierr = MatGetRow(matA, i, &(nnz[i]), NULL, NULL);CHKERRQ(ierr); 1293 PetscCheckFalse(nnz[i] % Nk,PETSC_COMM_SELF, PETSC_ERR_PLIB, "nonzeros in matA are not in k-form size blocks"); 1294 maxnnz = PetscMax(maxnnz, nnz[i]); 1295 } 1296 for (i = 0; i < mB; i++) { 1297 ierr = MatGetRow(matB, i, &(nnz[i+mA]), NULL, NULL);CHKERRQ(ierr); 1298 PetscCheckFalse(nnz[i+mA] % Nk,PETSC_COMM_SELF, PETSC_ERR_PLIB, "nonzeros in matB are not in k-form size blocks"); 1299 maxnnz = PetscMax(maxnnz, nnz[i+mA]); 1300 } 1301 ierr = MatCreateSeqAIJ(PETSC_COMM_SELF, m, n, 0, nnz, &M);CHKERRQ(ierr); 1302 ierr = PetscFree(nnz);CHKERRQ(ierr); 1303 /* reasoning about which points each dof needs depends on having zeros computed at points preserved */ 1304 ierr = MatSetOption(M, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr); 1305 ierr = PetscMalloc1(maxnnz, &work);CHKERRQ(ierr); 1306 for (i = 0; i < mA; i++) { 1307 const PetscInt *cols; 1308 const PetscScalar *vals; 1309 PetscInt nCols; 1310 ierr = MatGetRow(matA, i, &nCols, &cols, &vals);CHKERRQ(ierr); 1311 for (j = 0; j < nCols / Nk; j++) { 1312 PetscInt newCol = aToMerged[cols[j * Nk] / Nk]; 1313 for (l = 0; l < Nk; l++) work[j * Nk + l] = newCol * Nk + l; 1314 } 1315 ierr = MatSetValuesBlocked(M, 1, &i, nCols, work, vals, INSERT_VALUES);CHKERRQ(ierr); 1316 ierr = MatRestoreRow(matA, i, &nCols, &cols, &vals);CHKERRQ(ierr); 1317 } 1318 for (i = 0; i < mB; i++) { 1319 const PetscInt *cols; 1320 const PetscScalar *vals; 1321 1322 PetscInt row = i + mA; 1323 PetscInt nCols; 1324 ierr = MatGetRow(matB, i, &nCols, &cols, &vals);CHKERRQ(ierr); 1325 for (j = 0; j < nCols / Nk; j++) { 1326 PetscInt newCol = bToMerged[cols[j * Nk] / Nk]; 1327 for (l = 0; l < Nk; l++) work[j * Nk + l] = newCol * Nk + l; 1328 } 1329 ierr = MatSetValuesBlocked(M, 1, &row, nCols, work, vals, INSERT_VALUES);CHKERRQ(ierr); 1330 ierr = MatRestoreRow(matB, i, &nCols, &cols, &vals);CHKERRQ(ierr); 1331 } 1332 ierr = PetscFree(work);CHKERRQ(ierr); 1333 ierr = MatAssemblyBegin(M, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1334 ierr = MatAssemblyEnd(M, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1335 *matMerged = M; 1336 PetscFunctionReturn(0); 1337 } 1338 1339 /* Take a dual space and product a segment space that has all the same specifications (trimmed, continuous, order, 1340 * node set), except for the form degree. For computing boundary dofs and for making tensor product spaces */ 1341 static PetscErrorCode PetscDualSpaceCreateFacetSubspace_Lagrange(PetscDualSpace sp, DM K, PetscInt f, PetscInt k, PetscInt Ncopies, PetscBool interiorOnly, PetscDualSpace *bdsp) 1342 { 1343 PetscInt Nknew, Ncnew; 1344 PetscInt dim, pointDim = -1; 1345 PetscInt depth; 1346 DM dm; 1347 PetscDualSpace_Lag *newlag; 1348 PetscErrorCode ierr; 1349 1350 PetscFunctionBegin; 1351 ierr = PetscDualSpaceGetDM(sp,&dm);CHKERRQ(ierr); 1352 ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr); 1353 ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr); 1354 ierr = PetscDualSpaceDuplicate(sp,bdsp);CHKERRQ(ierr); 1355 ierr = PetscDualSpaceSetFormDegree(*bdsp,k);CHKERRQ(ierr); 1356 if (!K) { 1357 if (depth == dim) { 1358 DMPolytopeType ct; 1359 1360 pointDim = dim - 1; 1361 ierr = DMPlexGetCellType(dm, f, &ct);CHKERRQ(ierr); 1362 ierr = DMPlexCreateReferenceCell(PETSC_COMM_SELF, ct, &K);CHKERRQ(ierr); 1363 } else if (depth == 1) { 1364 pointDim = 0; 1365 ierr = DMPlexCreateReferenceCell(PETSC_COMM_SELF, DM_POLYTOPE_POINT, &K);CHKERRQ(ierr); 1366 } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unsupported interpolation state of reference element"); 1367 } else { 1368 ierr = PetscObjectReference((PetscObject)K);CHKERRQ(ierr); 1369 ierr = DMGetDimension(K, &pointDim);CHKERRQ(ierr); 1370 } 1371 ierr = PetscDualSpaceSetDM(*bdsp, K);CHKERRQ(ierr); 1372 ierr = DMDestroy(&K);CHKERRQ(ierr); 1373 ierr = PetscDTBinomialInt(pointDim, PetscAbsInt(k), &Nknew);CHKERRQ(ierr); 1374 Ncnew = Nknew * Ncopies; 1375 ierr = PetscDualSpaceSetNumComponents(*bdsp, Ncnew);CHKERRQ(ierr); 1376 newlag = (PetscDualSpace_Lag *) (*bdsp)->data; 1377 newlag->interiorOnly = interiorOnly; 1378 ierr = PetscDualSpaceSetUp(*bdsp);CHKERRQ(ierr); 1379 PetscFunctionReturn(0); 1380 } 1381 1382 /* Construct simplex nodes from a nodefamily, add Nk dof vectors of length Nk at each node. 1383 * Return the (quadrature, matrix) form of the dofs and the nodeIndices form as well. 1384 * 1385 * Sometimes we want a set of nodes to be contained in the interior of the element, 1386 * even when the node scheme puts nodes on the boundaries. numNodeSkip tells 1387 * the routine how many "layers" of nodes need to be skipped. 1388 * */ 1389 static PetscErrorCode PetscDualSpaceLagrangeCreateSimplexNodeMat(Petsc1DNodeFamily nodeFamily, PetscInt dim, PetscInt sum, PetscInt Nk, PetscInt numNodeSkip, PetscQuadrature *iNodes, Mat *iMat, PetscLagNodeIndices *nodeIndices) 1390 { 1391 PetscReal *extraNodeCoords, *nodeCoords; 1392 PetscInt nNodes, nExtraNodes; 1393 PetscInt i, j, k, extraSum = sum + numNodeSkip * (1 + dim); 1394 PetscQuadrature intNodes; 1395 Mat intMat; 1396 PetscLagNodeIndices ni; 1397 PetscErrorCode ierr; 1398 1399 PetscFunctionBegin; 1400 ierr = PetscDTBinomialInt(dim + sum, dim, &nNodes);CHKERRQ(ierr); 1401 ierr = PetscDTBinomialInt(dim + extraSum, dim, &nExtraNodes);CHKERRQ(ierr); 1402 1403 ierr = PetscMalloc1(dim * nExtraNodes, &extraNodeCoords);CHKERRQ(ierr); 1404 ierr = PetscNew(&ni);CHKERRQ(ierr); 1405 ni->nodeIdxDim = dim + 1; 1406 ni->nodeVecDim = Nk; 1407 ni->nNodes = nNodes * Nk; 1408 ni->refct = 1; 1409 ierr = PetscMalloc1(nNodes * Nk * (dim + 1), &(ni->nodeIdx));CHKERRQ(ierr); 1410 ierr = PetscMalloc1(nNodes * Nk * Nk, &(ni->nodeVec));CHKERRQ(ierr); 1411 for (i = 0; i < nNodes; i++) for (j = 0; j < Nk; j++) for (k = 0; k < Nk; k++) ni->nodeVec[(i * Nk + j) * Nk + k] = (j == k) ? 1. : 0.; 1412 ierr = Petsc1DNodeFamilyComputeSimplexNodes(nodeFamily, dim, extraSum, extraNodeCoords);CHKERRQ(ierr); 1413 if (numNodeSkip) { 1414 PetscInt k; 1415 PetscInt *tup; 1416 1417 ierr = PetscMalloc1(dim * nNodes, &nodeCoords);CHKERRQ(ierr); 1418 ierr = PetscMalloc1(dim + 1, &tup);CHKERRQ(ierr); 1419 for (k = 0; k < nNodes; k++) { 1420 PetscInt j, c; 1421 PetscInt index; 1422 1423 ierr = PetscDTIndexToBary(dim + 1, sum, k, tup);CHKERRQ(ierr); 1424 for (j = 0; j < dim + 1; j++) tup[j] += numNodeSkip; 1425 for (c = 0; c < Nk; c++) { 1426 for (j = 0; j < dim + 1; j++) { 1427 ni->nodeIdx[(k * Nk + c) * (dim + 1) + j] = tup[j] + 1; 1428 } 1429 } 1430 ierr = PetscDTBaryToIndex(dim + 1, extraSum, tup, &index);CHKERRQ(ierr); 1431 for (j = 0; j < dim; j++) nodeCoords[k * dim + j] = extraNodeCoords[index * dim + j]; 1432 } 1433 ierr = PetscFree(tup);CHKERRQ(ierr); 1434 ierr = PetscFree(extraNodeCoords);CHKERRQ(ierr); 1435 } else { 1436 PetscInt k; 1437 PetscInt *tup; 1438 1439 nodeCoords = extraNodeCoords; 1440 ierr = PetscMalloc1(dim + 1, &tup);CHKERRQ(ierr); 1441 for (k = 0; k < nNodes; k++) { 1442 PetscInt j, c; 1443 1444 ierr = PetscDTIndexToBary(dim + 1, sum, k, tup);CHKERRQ(ierr); 1445 for (c = 0; c < Nk; c++) { 1446 for (j = 0; j < dim + 1; j++) { 1447 /* barycentric indices can have zeros, but we don't want to push forward zeros because it makes it harder to 1448 * determine which nodes correspond to which under symmetries, so we increase by 1. This is fine 1449 * because the nodeIdx coordinates don't have any meaning other than helping to identify symmetries */ 1450 ni->nodeIdx[(k * Nk + c) * (dim + 1) + j] = tup[j] + 1; 1451 } 1452 } 1453 } 1454 ierr = PetscFree(tup);CHKERRQ(ierr); 1455 } 1456 ierr = PetscQuadratureCreate(PETSC_COMM_SELF, &intNodes);CHKERRQ(ierr); 1457 ierr = PetscQuadratureSetData(intNodes, dim, 0, nNodes, nodeCoords, NULL);CHKERRQ(ierr); 1458 ierr = MatCreateSeqAIJ(PETSC_COMM_SELF, nNodes * Nk, nNodes * Nk, Nk, NULL, &intMat);CHKERRQ(ierr); 1459 ierr = MatSetOption(intMat,MAT_IGNORE_ZERO_ENTRIES,PETSC_FALSE);CHKERRQ(ierr); 1460 for (j = 0; j < nNodes * Nk; j++) { 1461 PetscInt rem = j % Nk; 1462 PetscInt a, aprev = j - rem; 1463 PetscInt anext = aprev + Nk; 1464 1465 for (a = aprev; a < anext; a++) { 1466 ierr = MatSetValue(intMat, j, a, (a == j) ? 1. : 0., INSERT_VALUES);CHKERRQ(ierr); 1467 } 1468 } 1469 ierr = MatAssemblyBegin(intMat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1470 ierr = MatAssemblyEnd(intMat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1471 *iNodes = intNodes; 1472 *iMat = intMat; 1473 *nodeIndices = ni; 1474 PetscFunctionReturn(0); 1475 } 1476 1477 /* once the nodeIndices have been created for the interior of the reference cell, and for all of the boundary cells, 1478 * push forward the boundary dofs and concatenate them into the full node indices for the dual space */ 1479 static PetscErrorCode PetscDualSpaceLagrangeCreateAllNodeIdx(PetscDualSpace sp) 1480 { 1481 DM dm; 1482 PetscInt dim, nDofs; 1483 PetscSection section; 1484 PetscInt pStart, pEnd, p; 1485 PetscInt formDegree, Nk; 1486 PetscInt nodeIdxDim, spintdim; 1487 PetscDualSpace_Lag *lag; 1488 PetscLagNodeIndices ni, verti; 1489 PetscErrorCode ierr; 1490 1491 PetscFunctionBegin; 1492 lag = (PetscDualSpace_Lag *) sp->data; 1493 verti = lag->vertIndices; 1494 ierr = PetscDualSpaceGetDM(sp, &dm);CHKERRQ(ierr); 1495 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1496 ierr = PetscDualSpaceGetFormDegree(sp, &formDegree);CHKERRQ(ierr); 1497 ierr = PetscDTBinomialInt(dim, PetscAbsInt(formDegree), &Nk);CHKERRQ(ierr); 1498 ierr = PetscDualSpaceGetSection(sp, §ion);CHKERRQ(ierr); 1499 ierr = PetscSectionGetStorageSize(section, &nDofs);CHKERRQ(ierr); 1500 ierr = PetscNew(&ni);CHKERRQ(ierr); 1501 ni->nodeIdxDim = nodeIdxDim = verti->nodeIdxDim; 1502 ni->nodeVecDim = Nk; 1503 ni->nNodes = nDofs; 1504 ni->refct = 1; 1505 ierr = PetscMalloc1(nodeIdxDim * nDofs, &(ni->nodeIdx));CHKERRQ(ierr); 1506 ierr = PetscMalloc1(Nk * nDofs, &(ni->nodeVec));CHKERRQ(ierr); 1507 ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 1508 ierr = PetscSectionGetDof(section, 0, &spintdim);CHKERRQ(ierr); 1509 if (spintdim) { 1510 ierr = PetscArraycpy(ni->nodeIdx, lag->intNodeIndices->nodeIdx, spintdim * nodeIdxDim);CHKERRQ(ierr); 1511 ierr = PetscArraycpy(ni->nodeVec, lag->intNodeIndices->nodeVec, spintdim * Nk);CHKERRQ(ierr); 1512 } 1513 for (p = pStart + 1; p < pEnd; p++) { 1514 PetscDualSpace psp = sp->pointSpaces[p]; 1515 PetscDualSpace_Lag *plag; 1516 PetscInt dof, off; 1517 1518 ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr); 1519 if (!dof) continue; 1520 plag = (PetscDualSpace_Lag *) psp->data; 1521 ierr = PetscSectionGetOffset(section, p, &off);CHKERRQ(ierr); 1522 ierr = PetscLagNodeIndicesPushForward(dm, verti, p, plag->vertIndices, plag->intNodeIndices, 0, formDegree, &(ni->nodeIdx[off * nodeIdxDim]), &(ni->nodeVec[off * Nk]));CHKERRQ(ierr); 1523 } 1524 lag->allNodeIndices = ni; 1525 PetscFunctionReturn(0); 1526 } 1527 1528 /* once the (quadrature, Matrix) forms of the dofs have been created for the interior of the 1529 * reference cell and for the boundary cells, jk 1530 * push forward the boundary data and concatenate them into the full (quadrature, matrix) data 1531 * for the dual space */ 1532 static PetscErrorCode PetscDualSpaceCreateAllDataFromInteriorData(PetscDualSpace sp) 1533 { 1534 DM dm; 1535 PetscSection section; 1536 PetscInt pStart, pEnd, p, k, Nk, dim, Nc; 1537 PetscInt nNodes; 1538 PetscInt countNodes; 1539 Mat allMat; 1540 PetscQuadrature allNodes; 1541 PetscInt nDofs; 1542 PetscInt maxNzforms, j; 1543 PetscScalar *work; 1544 PetscReal *L, *J, *Jinv, *v0, *pv0; 1545 PetscInt *iwork; 1546 PetscReal *nodes; 1547 PetscErrorCode ierr; 1548 1549 PetscFunctionBegin; 1550 ierr = PetscDualSpaceGetDM(sp, &dm);CHKERRQ(ierr); 1551 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1552 ierr = PetscDualSpaceGetSection(sp, §ion);CHKERRQ(ierr); 1553 ierr = PetscSectionGetStorageSize(section, &nDofs);CHKERRQ(ierr); 1554 ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 1555 ierr = PetscDualSpaceGetFormDegree(sp, &k);CHKERRQ(ierr); 1556 ierr = PetscDualSpaceGetNumComponents(sp, &Nc);CHKERRQ(ierr); 1557 ierr = PetscDTBinomialInt(dim, PetscAbsInt(k), &Nk);CHKERRQ(ierr); 1558 for (p = pStart, nNodes = 0, maxNzforms = 0; p < pEnd; p++) { 1559 PetscDualSpace psp; 1560 DM pdm; 1561 PetscInt pdim, pNk; 1562 PetscQuadrature intNodes; 1563 Mat intMat; 1564 1565 ierr = PetscDualSpaceGetPointSubspace(sp, p, &psp);CHKERRQ(ierr); 1566 if (!psp) continue; 1567 ierr = PetscDualSpaceGetDM(psp, &pdm);CHKERRQ(ierr); 1568 ierr = DMGetDimension(pdm, &pdim);CHKERRQ(ierr); 1569 if (pdim < PetscAbsInt(k)) continue; 1570 ierr = PetscDTBinomialInt(pdim, PetscAbsInt(k), &pNk);CHKERRQ(ierr); 1571 ierr = PetscDualSpaceGetInteriorData(psp, &intNodes, &intMat);CHKERRQ(ierr); 1572 if (intNodes) { 1573 PetscInt nNodesp; 1574 1575 ierr = PetscQuadratureGetData(intNodes, NULL, NULL, &nNodesp, NULL, NULL);CHKERRQ(ierr); 1576 nNodes += nNodesp; 1577 } 1578 if (intMat) { 1579 PetscInt maxNzsp; 1580 PetscInt maxNzformsp; 1581 1582 ierr = MatSeqAIJGetMaxRowNonzeros(intMat, &maxNzsp);CHKERRQ(ierr); 1583 PetscCheckFalse(maxNzsp % pNk,PETSC_COMM_SELF, PETSC_ERR_PLIB, "interior matrix is not laid out as blocks of k-forms"); 1584 maxNzformsp = maxNzsp / pNk; 1585 maxNzforms = PetscMax(maxNzforms, maxNzformsp); 1586 } 1587 } 1588 ierr = MatCreateSeqAIJ(PETSC_COMM_SELF, nDofs, nNodes * Nc, maxNzforms * Nk, NULL, &allMat);CHKERRQ(ierr); 1589 ierr = MatSetOption(allMat,MAT_IGNORE_ZERO_ENTRIES,PETSC_FALSE);CHKERRQ(ierr); 1590 ierr = PetscMalloc7(dim, &v0, dim, &pv0, dim * dim, &J, dim * dim, &Jinv, Nk * Nk, &L, maxNzforms * Nk, &work, maxNzforms * Nk, &iwork);CHKERRQ(ierr); 1591 for (j = 0; j < dim; j++) pv0[j] = -1.; 1592 ierr = PetscMalloc1(dim * nNodes, &nodes);CHKERRQ(ierr); 1593 for (p = pStart, countNodes = 0; p < pEnd; p++) { 1594 PetscDualSpace psp; 1595 PetscQuadrature intNodes; 1596 DM pdm; 1597 PetscInt pdim, pNk; 1598 PetscInt countNodesIn = countNodes; 1599 PetscReal detJ; 1600 Mat intMat; 1601 1602 ierr = PetscDualSpaceGetPointSubspace(sp, p, &psp);CHKERRQ(ierr); 1603 if (!psp) continue; 1604 ierr = PetscDualSpaceGetDM(psp, &pdm);CHKERRQ(ierr); 1605 ierr = DMGetDimension(pdm, &pdim);CHKERRQ(ierr); 1606 if (pdim < PetscAbsInt(k)) continue; 1607 ierr = PetscDualSpaceGetInteriorData(psp, &intNodes, &intMat);CHKERRQ(ierr); 1608 if (intNodes == NULL && intMat == NULL) continue; 1609 ierr = PetscDTBinomialInt(pdim, PetscAbsInt(k), &pNk);CHKERRQ(ierr); 1610 if (p) { 1611 ierr = DMPlexComputeCellGeometryAffineFEM(dm, p, v0, J, Jinv, &detJ);CHKERRQ(ierr); 1612 } else { /* identity */ 1613 PetscInt i,j; 1614 1615 for (i = 0; i < dim; i++) for (j = 0; j < dim; j++) J[i * dim + j] = Jinv[i * dim + j] = 0.; 1616 for (i = 0; i < dim; i++) J[i * dim + i] = Jinv[i * dim + i] = 1.; 1617 for (i = 0; i < dim; i++) v0[i] = -1.; 1618 } 1619 if (pdim != dim) { /* compactify Jacobian */ 1620 PetscInt i, j; 1621 1622 for (i = 0; i < dim; i++) for (j = 0; j < pdim; j++) J[i * pdim + j] = J[i * dim + j]; 1623 } 1624 ierr = PetscDTAltVPullbackMatrix(pdim, dim, J, k, L);CHKERRQ(ierr); 1625 if (intNodes) { /* push forward quadrature locations by the affine transformation */ 1626 PetscInt nNodesp; 1627 const PetscReal *nodesp; 1628 PetscInt j; 1629 1630 ierr = PetscQuadratureGetData(intNodes, NULL, NULL, &nNodesp, &nodesp, NULL);CHKERRQ(ierr); 1631 for (j = 0; j < nNodesp; j++, countNodes++) { 1632 PetscInt d, e; 1633 1634 for (d = 0; d < dim; d++) { 1635 nodes[countNodes * dim + d] = v0[d]; 1636 for (e = 0; e < pdim; e++) { 1637 nodes[countNodes * dim + d] += J[d * pdim + e] * (nodesp[j * pdim + e] - pv0[e]); 1638 } 1639 } 1640 } 1641 } 1642 if (intMat) { 1643 PetscInt nrows; 1644 PetscInt off; 1645 1646 ierr = PetscSectionGetDof(section, p, &nrows);CHKERRQ(ierr); 1647 ierr = PetscSectionGetOffset(section, p, &off);CHKERRQ(ierr); 1648 for (j = 0; j < nrows; j++) { 1649 PetscInt ncols; 1650 const PetscInt *cols; 1651 const PetscScalar *vals; 1652 PetscInt l, d, e; 1653 PetscInt row = j + off; 1654 1655 ierr = MatGetRow(intMat, j, &ncols, &cols, &vals);CHKERRQ(ierr); 1656 PetscCheckFalse(ncols % pNk,PETSC_COMM_SELF, PETSC_ERR_PLIB, "interior matrix is not laid out as blocks of k-forms"); 1657 for (l = 0; l < ncols / pNk; l++) { 1658 PetscInt blockcol; 1659 1660 for (d = 0; d < pNk; d++) { 1661 PetscCheckFalse((cols[l * pNk + d] % pNk) != d,PETSC_COMM_SELF, PETSC_ERR_PLIB, "interior matrix is not laid out as blocks of k-forms"); 1662 } 1663 blockcol = cols[l * pNk] / pNk; 1664 for (d = 0; d < Nk; d++) { 1665 iwork[l * Nk + d] = (blockcol + countNodesIn) * Nk + d; 1666 } 1667 for (d = 0; d < Nk; d++) work[l * Nk + d] = 0.; 1668 for (d = 0; d < Nk; d++) { 1669 for (e = 0; e < pNk; e++) { 1670 /* "push forward" dof by pulling back a k-form to be evaluated on the point: multiply on the right by L */ 1671 work[l * Nk + d] += vals[l * pNk + e] * L[e * Nk + d]; 1672 } 1673 } 1674 } 1675 ierr = MatSetValues(allMat, 1, &row, (ncols / pNk) * Nk, iwork, work, INSERT_VALUES);CHKERRQ(ierr); 1676 ierr = MatRestoreRow(intMat, j, &ncols, &cols, &vals);CHKERRQ(ierr); 1677 } 1678 } 1679 } 1680 ierr = MatAssemblyBegin(allMat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1681 ierr = MatAssemblyEnd(allMat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1682 ierr = PetscQuadratureCreate(PETSC_COMM_SELF, &allNodes);CHKERRQ(ierr); 1683 ierr = PetscQuadratureSetData(allNodes, dim, 0, nNodes, nodes, NULL);CHKERRQ(ierr); 1684 ierr = PetscFree7(v0, pv0, J, Jinv, L, work, iwork);CHKERRQ(ierr); 1685 ierr = MatDestroy(&(sp->allMat));CHKERRQ(ierr); 1686 sp->allMat = allMat; 1687 ierr = PetscQuadratureDestroy(&(sp->allNodes));CHKERRQ(ierr); 1688 sp->allNodes = allNodes; 1689 PetscFunctionReturn(0); 1690 } 1691 1692 /* rather than trying to get all data from the functionals, we create 1693 * the functionals from rows of the quadrature -> dof matrix. 1694 * 1695 * Ideally most of the uses of PetscDualSpace in PetscFE will switch 1696 * to using intMat and allMat, so that the individual functionals 1697 * don't need to be constructed at all */ 1698 static PetscErrorCode PetscDualSpaceComputeFunctionalsFromAllData(PetscDualSpace sp) 1699 { 1700 PetscQuadrature allNodes; 1701 Mat allMat; 1702 PetscInt nDofs; 1703 PetscInt dim, k, Nk, Nc, f; 1704 DM dm; 1705 PetscInt nNodes, spdim; 1706 const PetscReal *nodes = NULL; 1707 PetscSection section; 1708 PetscBool useMoments; 1709 PetscErrorCode ierr; 1710 1711 PetscFunctionBegin; 1712 ierr = PetscDualSpaceGetDM(sp, &dm);CHKERRQ(ierr); 1713 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1714 ierr = PetscDualSpaceGetNumComponents(sp, &Nc);CHKERRQ(ierr); 1715 ierr = PetscDualSpaceGetFormDegree(sp, &k);CHKERRQ(ierr); 1716 ierr = PetscDTBinomialInt(dim, PetscAbsInt(k), &Nk);CHKERRQ(ierr); 1717 ierr = PetscDualSpaceGetAllData(sp, &allNodes, &allMat);CHKERRQ(ierr); 1718 nNodes = 0; 1719 if (allNodes) { 1720 ierr = PetscQuadratureGetData(allNodes, NULL, NULL, &nNodes, &nodes, NULL);CHKERRQ(ierr); 1721 } 1722 ierr = MatGetSize(allMat, &nDofs, NULL);CHKERRQ(ierr); 1723 ierr = PetscDualSpaceGetSection(sp, §ion);CHKERRQ(ierr); 1724 ierr = PetscSectionGetStorageSize(section, &spdim);CHKERRQ(ierr); 1725 PetscCheckFalse(spdim != nDofs,PETSC_COMM_SELF, PETSC_ERR_PLIB, "incompatible all matrix size"); 1726 ierr = PetscMalloc1(nDofs, &(sp->functional));CHKERRQ(ierr); 1727 ierr = PetscDualSpaceLagrangeGetUseMoments(sp, &useMoments);CHKERRQ(ierr); 1728 if (useMoments) { 1729 Mat allMat; 1730 PetscInt momentOrder, i; 1731 PetscBool tensor; 1732 const PetscReal *weights; 1733 PetscScalar *array; 1734 1735 PetscCheckFalse(nDofs != 1,PETSC_COMM_SELF, PETSC_ERR_SUP, "We do not yet support moments beyond P0, nDofs == %D", nDofs); 1736 ierr = PetscDualSpaceLagrangeGetMomentOrder(sp, &momentOrder);CHKERRQ(ierr); 1737 ierr = PetscDualSpaceLagrangeGetTensor(sp, &tensor);CHKERRQ(ierr); 1738 if (!tensor) {ierr = PetscDTStroudConicalQuadrature(dim, Nc, PetscMax(momentOrder + 1,1), -1.0, 1.0, &(sp->functional[0]));CHKERRQ(ierr);} 1739 else {ierr = PetscDTGaussTensorQuadrature(dim, Nc, PetscMax(momentOrder + 1,1), -1.0, 1.0, &(sp->functional[0]));CHKERRQ(ierr);} 1740 /* Need to replace allNodes and allMat */ 1741 ierr = PetscObjectReference((PetscObject) sp->functional[0]);CHKERRQ(ierr); 1742 ierr = PetscQuadratureDestroy(&(sp->allNodes));CHKERRQ(ierr); 1743 sp->allNodes = sp->functional[0]; 1744 ierr = PetscQuadratureGetData(sp->allNodes, NULL, NULL, &nNodes, NULL, &weights);CHKERRQ(ierr); 1745 ierr = MatCreateSeqDense(PETSC_COMM_SELF, nDofs, nNodes * Nc, NULL, &allMat);CHKERRQ(ierr); 1746 ierr = MatDenseGetArrayWrite(allMat, &array);CHKERRQ(ierr); 1747 for (i = 0; i < nNodes * Nc; ++i) array[i] = weights[i]; 1748 ierr = MatDenseRestoreArrayWrite(allMat, &array);CHKERRQ(ierr); 1749 ierr = MatAssemblyBegin(allMat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1750 ierr = MatAssemblyEnd(allMat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1751 ierr = MatDestroy(&(sp->allMat));CHKERRQ(ierr); 1752 sp->allMat = allMat; 1753 PetscFunctionReturn(0); 1754 } 1755 for (f = 0; f < nDofs; f++) { 1756 PetscInt ncols, c; 1757 const PetscInt *cols; 1758 const PetscScalar *vals; 1759 PetscReal *nodesf; 1760 PetscReal *weightsf; 1761 PetscInt nNodesf; 1762 PetscInt countNodes; 1763 1764 ierr = MatGetRow(allMat, f, &ncols, &cols, &vals);CHKERRQ(ierr); 1765 PetscCheckFalse(ncols % Nk,PETSC_COMM_SELF, PETSC_ERR_PLIB, "all matrix is not laid out as blocks of k-forms"); 1766 for (c = 1, nNodesf = 1; c < ncols; c++) { 1767 if ((cols[c] / Nc) != (cols[c-1] / Nc)) nNodesf++; 1768 } 1769 ierr = PetscMalloc1(dim * nNodesf, &nodesf);CHKERRQ(ierr); 1770 ierr = PetscMalloc1(Nc * nNodesf, &weightsf);CHKERRQ(ierr); 1771 for (c = 0, countNodes = 0; c < ncols; c++) { 1772 if (!c || ((cols[c] / Nc) != (cols[c-1] / Nc))) { 1773 PetscInt d; 1774 1775 for (d = 0; d < Nc; d++) { 1776 weightsf[countNodes * Nc + d] = 0.; 1777 } 1778 for (d = 0; d < dim; d++) { 1779 nodesf[countNodes * dim + d] = nodes[(cols[c] / Nc) * dim + d]; 1780 } 1781 countNodes++; 1782 } 1783 weightsf[(countNodes - 1) * Nc + (cols[c] % Nc)] = PetscRealPart(vals[c]); 1784 } 1785 ierr = PetscQuadratureCreate(PETSC_COMM_SELF, &(sp->functional[f]));CHKERRQ(ierr); 1786 ierr = PetscQuadratureSetData(sp->functional[f], dim, Nc, nNodesf, nodesf, weightsf);CHKERRQ(ierr); 1787 ierr = MatRestoreRow(allMat, f, &ncols, &cols, &vals);CHKERRQ(ierr); 1788 } 1789 PetscFunctionReturn(0); 1790 } 1791 1792 /* take a matrix meant for k-forms and expand it to one for Ncopies */ 1793 static PetscErrorCode PetscDualSpaceLagrangeMatrixCreateCopies(Mat A, PetscInt Nk, PetscInt Ncopies, Mat *Abs) 1794 { 1795 PetscInt m, n, i, j, k; 1796 PetscInt maxnnz, *nnz, *iwork; 1797 Mat Ac; 1798 PetscErrorCode ierr; 1799 1800 PetscFunctionBegin; 1801 ierr = MatGetSize(A, &m, &n);CHKERRQ(ierr); 1802 PetscCheckFalse(n % Nk,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of columns in A %D is not a multiple of Nk %D", n, Nk); 1803 ierr = PetscMalloc1(m * Ncopies, &nnz);CHKERRQ(ierr); 1804 for (i = 0, maxnnz = 0; i < m; i++) { 1805 PetscInt innz; 1806 ierr = MatGetRow(A, i, &innz, NULL, NULL);CHKERRQ(ierr); 1807 PetscCheckFalse(innz % Nk,PETSC_COMM_SELF, PETSC_ERR_PLIB, "A row %D nnzs is not a multiple of Nk %D", innz, Nk); 1808 for (j = 0; j < Ncopies; j++) nnz[i * Ncopies + j] = innz; 1809 maxnnz = PetscMax(maxnnz, innz); 1810 } 1811 ierr = MatCreateSeqAIJ(PETSC_COMM_SELF, m * Ncopies, n * Ncopies, 0, nnz, &Ac);CHKERRQ(ierr); 1812 ierr = MatSetOption(Ac, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr); 1813 ierr = PetscFree(nnz);CHKERRQ(ierr); 1814 ierr = PetscMalloc1(maxnnz, &iwork);CHKERRQ(ierr); 1815 for (i = 0; i < m; i++) { 1816 PetscInt innz; 1817 const PetscInt *cols; 1818 const PetscScalar *vals; 1819 1820 ierr = MatGetRow(A, i, &innz, &cols, &vals);CHKERRQ(ierr); 1821 for (j = 0; j < innz; j++) iwork[j] = (cols[j] / Nk) * (Nk * Ncopies) + (cols[j] % Nk); 1822 for (j = 0; j < Ncopies; j++) { 1823 PetscInt row = i * Ncopies + j; 1824 1825 ierr = MatSetValues(Ac, 1, &row, innz, iwork, vals, INSERT_VALUES);CHKERRQ(ierr); 1826 for (k = 0; k < innz; k++) iwork[k] += Nk; 1827 } 1828 ierr = MatRestoreRow(A, i, &innz, &cols, &vals);CHKERRQ(ierr); 1829 } 1830 ierr = PetscFree(iwork);CHKERRQ(ierr); 1831 ierr = MatAssemblyBegin(Ac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1832 ierr = MatAssemblyEnd(Ac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1833 *Abs = Ac; 1834 PetscFunctionReturn(0); 1835 } 1836 1837 /* check if a cell is a tensor product of the segment with a facet, 1838 * specifically checking if f and f2 can be the "endpoints" (like the triangles 1839 * at either end of a wedge) */ 1840 static PetscErrorCode DMPlexPointIsTensor_Internal_Given(DM dm, PetscInt p, PetscInt f, PetscInt f2, PetscBool *isTensor) 1841 { 1842 PetscInt coneSize, c; 1843 const PetscInt *cone; 1844 const PetscInt *fCone; 1845 const PetscInt *f2Cone; 1846 PetscInt fs[2]; 1847 PetscInt meetSize, nmeet; 1848 const PetscInt *meet; 1849 PetscErrorCode ierr; 1850 1851 PetscFunctionBegin; 1852 fs[0] = f; 1853 fs[1] = f2; 1854 ierr = DMPlexGetMeet(dm, 2, fs, &meetSize, &meet);CHKERRQ(ierr); 1855 nmeet = meetSize; 1856 ierr = DMPlexRestoreMeet(dm, 2, fs, &meetSize, &meet);CHKERRQ(ierr); 1857 /* two points that have a non-empty meet cannot be at opposite ends of a cell */ 1858 if (nmeet) { 1859 *isTensor = PETSC_FALSE; 1860 PetscFunctionReturn(0); 1861 } 1862 ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr); 1863 ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr); 1864 ierr = DMPlexGetCone(dm, f, &fCone);CHKERRQ(ierr); 1865 ierr = DMPlexGetCone(dm, f2, &f2Cone);CHKERRQ(ierr); 1866 for (c = 0; c < coneSize; c++) { 1867 PetscInt e, ef; 1868 PetscInt d = -1, d2 = -1; 1869 PetscInt dcount, d2count; 1870 PetscInt t = cone[c]; 1871 PetscInt tConeSize; 1872 PetscBool tIsTensor; 1873 const PetscInt *tCone; 1874 1875 if (t == f || t == f2) continue; 1876 /* for every other facet in the cone, check that is has 1877 * one ridge in common with each end */ 1878 ierr = DMPlexGetConeSize(dm, t, &tConeSize);CHKERRQ(ierr); 1879 ierr = DMPlexGetCone(dm, t, &tCone);CHKERRQ(ierr); 1880 1881 dcount = 0; 1882 d2count = 0; 1883 for (e = 0; e < tConeSize; e++) { 1884 PetscInt q = tCone[e]; 1885 for (ef = 0; ef < coneSize - 2; ef++) { 1886 if (fCone[ef] == q) { 1887 if (dcount) { 1888 *isTensor = PETSC_FALSE; 1889 PetscFunctionReturn(0); 1890 } 1891 d = q; 1892 dcount++; 1893 } else if (f2Cone[ef] == q) { 1894 if (d2count) { 1895 *isTensor = PETSC_FALSE; 1896 PetscFunctionReturn(0); 1897 } 1898 d2 = q; 1899 d2count++; 1900 } 1901 } 1902 } 1903 /* if the whole cell is a tensor with the segment, then this 1904 * facet should be a tensor with the segment */ 1905 ierr = DMPlexPointIsTensor_Internal_Given(dm, t, d, d2, &tIsTensor);CHKERRQ(ierr); 1906 if (!tIsTensor) { 1907 *isTensor = PETSC_FALSE; 1908 PetscFunctionReturn(0); 1909 } 1910 } 1911 *isTensor = PETSC_TRUE; 1912 PetscFunctionReturn(0); 1913 } 1914 1915 /* determine if a cell is a tensor with a segment by looping over pairs of facets to find a pair 1916 * that could be the opposite ends */ 1917 static PetscErrorCode DMPlexPointIsTensor_Internal(DM dm, PetscInt p, PetscBool *isTensor, PetscInt *endA, PetscInt *endB) 1918 { 1919 PetscInt coneSize, c, c2; 1920 const PetscInt *cone; 1921 PetscErrorCode ierr; 1922 1923 PetscFunctionBegin; 1924 ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr); 1925 if (!coneSize) { 1926 if (isTensor) *isTensor = PETSC_FALSE; 1927 if (endA) *endA = -1; 1928 if (endB) *endB = -1; 1929 } 1930 ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr); 1931 for (c = 0; c < coneSize; c++) { 1932 PetscInt f = cone[c]; 1933 PetscInt fConeSize; 1934 1935 ierr = DMPlexGetConeSize(dm, f, &fConeSize);CHKERRQ(ierr); 1936 if (fConeSize != coneSize - 2) continue; 1937 1938 for (c2 = c + 1; c2 < coneSize; c2++) { 1939 PetscInt f2 = cone[c2]; 1940 PetscBool isTensorff2; 1941 PetscInt f2ConeSize; 1942 1943 ierr = DMPlexGetConeSize(dm, f2, &f2ConeSize);CHKERRQ(ierr); 1944 if (f2ConeSize != coneSize - 2) continue; 1945 1946 ierr = DMPlexPointIsTensor_Internal_Given(dm, p, f, f2, &isTensorff2);CHKERRQ(ierr); 1947 if (isTensorff2) { 1948 if (isTensor) *isTensor = PETSC_TRUE; 1949 if (endA) *endA = f; 1950 if (endB) *endB = f2; 1951 PetscFunctionReturn(0); 1952 } 1953 } 1954 } 1955 if (isTensor) *isTensor = PETSC_FALSE; 1956 if (endA) *endA = -1; 1957 if (endB) *endB = -1; 1958 PetscFunctionReturn(0); 1959 } 1960 1961 /* determine if a cell is a tensor with a segment by looping over pairs of facets to find a pair 1962 * that could be the opposite ends */ 1963 static PetscErrorCode DMPlexPointIsTensor(DM dm, PetscInt p, PetscBool *isTensor, PetscInt *endA, PetscInt *endB) 1964 { 1965 DMPlexInterpolatedFlag interpolated; 1966 PetscErrorCode ierr; 1967 1968 PetscFunctionBegin; 1969 ierr = DMPlexIsInterpolated(dm, &interpolated);CHKERRQ(ierr); 1970 PetscCheckFalse(interpolated != DMPLEX_INTERPOLATED_FULL,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Only for interpolated DMPlex's"); 1971 ierr = DMPlexPointIsTensor_Internal(dm, p, isTensor, endA, endB);CHKERRQ(ierr); 1972 PetscFunctionReturn(0); 1973 } 1974 1975 /* Let k = formDegree and k' = -sign(k) * dim + k. Transform a symmetric frame for k-forms on the biunit simplex into 1976 * a symmetric frame for k'-forms on the biunit simplex. 1977 * 1978 * A frame is "symmetric" if the pullback of every symmetry of the biunit simplex is a permutation of the frame. 1979 * 1980 * forms in the symmetric frame are used as dofs in the untrimmed simplex spaces. This way, symmetries of the 1981 * reference cell result in permutations of dofs grouped by node. 1982 * 1983 * Use T to transform dof matrices for k'-forms into dof matrices for k-forms as a block diagonal transformation on 1984 * the right. 1985 */ 1986 static PetscErrorCode BiunitSimplexSymmetricFormTransformation(PetscInt dim, PetscInt formDegree, PetscReal T[]) 1987 { 1988 PetscInt k = formDegree; 1989 PetscInt kd = k < 0 ? dim + k : k - dim; 1990 PetscInt Nk; 1991 PetscReal *biToEq, *eqToBi, *biToEqStar, *eqToBiStar; 1992 PetscInt fact; 1993 PetscErrorCode ierr; 1994 1995 PetscFunctionBegin; 1996 ierr = PetscDTBinomialInt(dim, PetscAbsInt(k), &Nk);CHKERRQ(ierr); 1997 ierr = PetscCalloc4(dim * dim, &biToEq, dim * dim, &eqToBi, Nk * Nk, &biToEqStar, Nk * Nk, &eqToBiStar);CHKERRQ(ierr); 1998 /* fill in biToEq: Jacobian of the transformation from the biunit simplex to the equilateral simplex */ 1999 fact = 0; 2000 for (PetscInt i = 0; i < dim; i++) { 2001 biToEq[i * dim + i] = PetscSqrtReal(((PetscReal)i + 2.) / (2.*((PetscReal)i+1.))); 2002 fact += 4*(i+1); 2003 for (PetscInt j = i+1; j < dim; j++) { 2004 biToEq[i * dim + j] = PetscSqrtReal(1./(PetscReal)fact); 2005 } 2006 } 2007 /* fill in eqToBi: Jacobian of the transformation from the equilateral simplex to the biunit simplex */ 2008 fact = 0; 2009 for (PetscInt j = 0; j < dim; j++) { 2010 eqToBi[j * dim + j] = PetscSqrtReal(2.*((PetscReal)j+1.)/((PetscReal)j+2)); 2011 fact += j+1; 2012 for (PetscInt i = 0; i < j; i++) { 2013 eqToBi[i * dim + j] = -PetscSqrtReal(1./(PetscReal)fact); 2014 } 2015 } 2016 ierr = PetscDTAltVPullbackMatrix(dim, dim, biToEq, kd, biToEqStar);CHKERRQ(ierr); 2017 ierr = PetscDTAltVPullbackMatrix(dim, dim, eqToBi, k, eqToBiStar);CHKERRQ(ierr); 2018 /* product of pullbacks simulates the following steps 2019 * 2020 * 1. start with frame W = [w_1, w_2, ..., w_m] of k forms that is symmetric on the biunit simplex: 2021 if J is the Jacobian of a symmetry of the biunit simplex, then J_k* W = [J_k*w_1, ..., J_k*w_m] 2022 is a permutation of W. 2023 Even though a k' form --- a (dim - k) form represented by its Hodge star --- has the same geometric 2024 content as a k form, W is not a symmetric frame of k' forms on the biunit simplex. That's because, 2025 for general Jacobian J, J_k* != J_k'*. 2026 * 2. pullback W to the equilateral triangle using the k pullback, W_eq = eqToBi_k* W. All symmetries of the 2027 equilateral simplex have orthonormal Jacobians. For an orthonormal Jacobian O, J_k* = J_k'*, so W_eq is 2028 also a symmetric frame for k' forms on the equilateral simplex. 2029 3. pullback W_eq back to the biunit simplex using the k' pulback, V = biToEq_k'* W_eq = biToEq_k'* eqToBi_k* W. 2030 V is a symmetric frame for k' forms on the biunit simplex. 2031 */ 2032 for (PetscInt i = 0; i < Nk; i++) { 2033 for (PetscInt j = 0; j < Nk; j++) { 2034 PetscReal val = 0.; 2035 for (PetscInt k = 0; k < Nk; k++) val += biToEqStar[i * Nk + k] * eqToBiStar[k * Nk + j]; 2036 T[i * Nk + j] = val; 2037 } 2038 } 2039 ierr = PetscFree4(biToEq, eqToBi, biToEqStar, eqToBiStar);CHKERRQ(ierr); 2040 PetscFunctionReturn(0); 2041 } 2042 2043 /* permute a quadrature -> dof matrix so that its rows are in revlex order by nodeIdx */ 2044 static PetscErrorCode MatPermuteByNodeIdx(Mat A, PetscLagNodeIndices ni, Mat *Aperm) 2045 { 2046 PetscInt m, n, i, j; 2047 PetscInt nodeIdxDim = ni->nodeIdxDim; 2048 PetscInt nodeVecDim = ni->nodeVecDim; 2049 PetscInt *perm; 2050 IS permIS; 2051 IS id; 2052 PetscInt *nIdxPerm; 2053 PetscReal *nVecPerm; 2054 PetscErrorCode ierr; 2055 2056 PetscFunctionBegin; 2057 ierr = PetscLagNodeIndicesGetPermutation(ni, &perm);CHKERRQ(ierr); 2058 ierr = MatGetSize(A, &m, &n);CHKERRQ(ierr); 2059 ierr = PetscMalloc1(nodeIdxDim * m, &nIdxPerm);CHKERRQ(ierr); 2060 ierr = PetscMalloc1(nodeVecDim * m, &nVecPerm);CHKERRQ(ierr); 2061 for (i = 0; i < m; i++) for (j = 0; j < nodeIdxDim; j++) nIdxPerm[i * nodeIdxDim + j] = ni->nodeIdx[perm[i] * nodeIdxDim + j]; 2062 for (i = 0; i < m; i++) for (j = 0; j < nodeVecDim; j++) nVecPerm[i * nodeVecDim + j] = ni->nodeVec[perm[i] * nodeVecDim + j]; 2063 ierr = ISCreateGeneral(PETSC_COMM_SELF, m, perm, PETSC_USE_POINTER, &permIS);CHKERRQ(ierr); 2064 ierr = ISSetPermutation(permIS);CHKERRQ(ierr); 2065 ierr = ISCreateStride(PETSC_COMM_SELF, n, 0, 1, &id);CHKERRQ(ierr); 2066 ierr = ISSetPermutation(id);CHKERRQ(ierr); 2067 ierr = MatPermute(A, permIS, id, Aperm);CHKERRQ(ierr); 2068 ierr = ISDestroy(&permIS);CHKERRQ(ierr); 2069 ierr = ISDestroy(&id);CHKERRQ(ierr); 2070 for (i = 0; i < m; i++) perm[i] = i; 2071 ierr = PetscFree(ni->nodeIdx);CHKERRQ(ierr); 2072 ierr = PetscFree(ni->nodeVec);CHKERRQ(ierr); 2073 ni->nodeIdx = nIdxPerm; 2074 ni->nodeVec = nVecPerm; 2075 PetscFunctionReturn(0); 2076 } 2077 2078 static PetscErrorCode PetscDualSpaceSetUp_Lagrange(PetscDualSpace sp) 2079 { 2080 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *) sp->data; 2081 DM dm = sp->dm; 2082 DM dmint = NULL; 2083 PetscInt order; 2084 PetscInt Nc = sp->Nc; 2085 MPI_Comm comm; 2086 PetscBool continuous; 2087 PetscSection section; 2088 PetscInt depth, dim, pStart, pEnd, cStart, cEnd, p, *pStratStart, *pStratEnd, d; 2089 PetscInt formDegree, Nk, Ncopies; 2090 PetscInt tensorf = -1, tensorf2 = -1; 2091 PetscBool tensorCell, tensorSpace; 2092 PetscBool uniform, trimmed; 2093 Petsc1DNodeFamily nodeFamily; 2094 PetscInt numNodeSkip; 2095 DMPlexInterpolatedFlag interpolated; 2096 PetscBool isbdm; 2097 PetscErrorCode ierr; 2098 2099 PetscFunctionBegin; 2100 /* step 1: sanitize input */ 2101 ierr = PetscObjectGetComm((PetscObject) sp, &comm);CHKERRQ(ierr); 2102 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 2103 ierr = PetscObjectTypeCompare((PetscObject)sp, PETSCDUALSPACEBDM, &isbdm);CHKERRQ(ierr); 2104 if (isbdm) { 2105 sp->k = -(dim-1); /* form degree of H-div */ 2106 ierr = PetscObjectChangeTypeName((PetscObject)sp, PETSCDUALSPACELAGRANGE);CHKERRQ(ierr); 2107 } 2108 ierr = PetscDualSpaceGetFormDegree(sp, &formDegree);CHKERRQ(ierr); 2109 PetscCheckFalse(PetscAbsInt(formDegree) > dim,comm, PETSC_ERR_ARG_OUTOFRANGE, "Form degree must be bounded by dimension"); 2110 ierr = PetscDTBinomialInt(dim,PetscAbsInt(formDegree),&Nk);CHKERRQ(ierr); 2111 if (sp->Nc <= 0 && lag->numCopies > 0) sp->Nc = Nk * lag->numCopies; 2112 Nc = sp->Nc; 2113 PetscCheckFalse(Nc % Nk,comm, PETSC_ERR_ARG_INCOMP, "Number of components is not a multiple of form degree size"); 2114 if (lag->numCopies <= 0) lag->numCopies = Nc / Nk; 2115 Ncopies = lag->numCopies; 2116 PetscCheckFalse(Nc / Nk != Ncopies,comm, PETSC_ERR_ARG_INCOMP, "Number of copies * (dim choose k) != Nc"); 2117 if (!dim) sp->order = 0; 2118 order = sp->order; 2119 uniform = sp->uniform; 2120 PetscCheckFalse(!uniform,PETSC_COMM_SELF, PETSC_ERR_SUP, "Variable order not supported yet"); 2121 if (lag->trimmed && !formDegree) lag->trimmed = PETSC_FALSE; /* trimmed spaces are the same as full spaces for 0-forms */ 2122 if (lag->nodeType == PETSCDTNODES_DEFAULT) { 2123 lag->nodeType = PETSCDTNODES_GAUSSJACOBI; 2124 lag->nodeExponent = 0.; 2125 /* trimmed spaces don't include corner vertices, so don't use end nodes by default */ 2126 lag->endNodes = lag->trimmed ? PETSC_FALSE : PETSC_TRUE; 2127 } 2128 /* If a trimmed space and the user did choose nodes with endpoints, skip them by default */ 2129 if (lag->numNodeSkip < 0) lag->numNodeSkip = (lag->trimmed && lag->endNodes) ? 1 : 0; 2130 numNodeSkip = lag->numNodeSkip; 2131 PetscCheckFalse(lag->trimmed && !order,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot have zeroth order trimmed elements"); 2132 if (lag->trimmed && PetscAbsInt(formDegree) == dim) { /* convert trimmed n-forms to untrimmed of one polynomial order less */ 2133 sp->order--; 2134 order--; 2135 lag->trimmed = PETSC_FALSE; 2136 } 2137 trimmed = lag->trimmed; 2138 if (!order || PetscAbsInt(formDegree) == dim) lag->continuous = PETSC_FALSE; 2139 continuous = lag->continuous; 2140 ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 2141 ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 2142 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 2143 PetscCheckFalse(pStart != 0 || cStart != 0,PetscObjectComm((PetscObject)sp), PETSC_ERR_ARG_WRONGSTATE, "Expect DM with chart starting at zero and cells first"); 2144 PetscCheckFalse(cEnd != 1,PetscObjectComm((PetscObject)sp), PETSC_ERR_ARG_WRONGSTATE, "Use PETSCDUALSPACEREFINED for multi-cell reference meshes"); 2145 ierr = DMPlexIsInterpolated(dm, &interpolated);CHKERRQ(ierr); 2146 if (interpolated != DMPLEX_INTERPOLATED_FULL) { 2147 ierr = DMPlexInterpolate(dm, &dmint);CHKERRQ(ierr); 2148 } else { 2149 ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 2150 dmint = dm; 2151 } 2152 tensorCell = PETSC_FALSE; 2153 if (dim > 1) { 2154 ierr = DMPlexPointIsTensor(dmint, 0, &tensorCell, &tensorf, &tensorf2);CHKERRQ(ierr); 2155 } 2156 lag->tensorCell = tensorCell; 2157 if (dim < 2 || !lag->tensorCell) lag->tensorSpace = PETSC_FALSE; 2158 tensorSpace = lag->tensorSpace; 2159 if (!lag->nodeFamily) { 2160 ierr = Petsc1DNodeFamilyCreate(lag->nodeType, lag->nodeExponent, lag->endNodes, &lag->nodeFamily);CHKERRQ(ierr); 2161 } 2162 nodeFamily = lag->nodeFamily; 2163 PetscCheckFalse(interpolated != DMPLEX_INTERPOLATED_FULL && continuous && (PetscAbsInt(formDegree) > 0 || order > 1),PETSC_COMM_SELF,PETSC_ERR_PLIB,"Reference element won't support all boundary nodes"); 2164 2165 /* step 2: construct the boundary spaces */ 2166 ierr = PetscMalloc2(depth+1,&pStratStart,depth+1,&pStratEnd);CHKERRQ(ierr); 2167 ierr = PetscCalloc1(pEnd,&(sp->pointSpaces));CHKERRQ(ierr); 2168 for (d = 0; d <= depth; ++d) {ierr = DMPlexGetDepthStratum(dm, d, &pStratStart[d], &pStratEnd[d]);CHKERRQ(ierr);} 2169 ierr = PetscDualSpaceSectionCreate_Internal(sp, §ion);CHKERRQ(ierr); 2170 sp->pointSection = section; 2171 if (continuous && !(lag->interiorOnly)) { 2172 PetscInt h; 2173 2174 for (p = pStratStart[depth - 1]; p < pStratEnd[depth - 1]; p++) { /* calculate the facet dual spaces */ 2175 PetscReal v0[3]; 2176 DMPolytopeType ptype; 2177 PetscReal J[9], detJ; 2178 PetscInt q; 2179 2180 ierr = DMPlexComputeCellGeometryAffineFEM(dm, p, v0, J, NULL, &detJ);CHKERRQ(ierr); 2181 ierr = DMPlexGetCellType(dm, p, &ptype);CHKERRQ(ierr); 2182 2183 /* compare to previous facets: if computed, reference that dualspace */ 2184 for (q = pStratStart[depth - 1]; q < p; q++) { 2185 DMPolytopeType qtype; 2186 2187 ierr = DMPlexGetCellType(dm, q, &qtype);CHKERRQ(ierr); 2188 if (qtype == ptype) break; 2189 } 2190 if (q < p) { /* this facet has the same dual space as that one */ 2191 ierr = PetscObjectReference((PetscObject)sp->pointSpaces[q]);CHKERRQ(ierr); 2192 sp->pointSpaces[p] = sp->pointSpaces[q]; 2193 continue; 2194 } 2195 /* if not, recursively compute this dual space */ 2196 ierr = PetscDualSpaceCreateFacetSubspace_Lagrange(sp,NULL,p,formDegree,Ncopies,PETSC_FALSE,&sp->pointSpaces[p]);CHKERRQ(ierr); 2197 } 2198 for (h = 2; h <= depth; h++) { /* get the higher subspaces from the facet subspaces */ 2199 PetscInt hd = depth - h; 2200 PetscInt hdim = dim - h; 2201 2202 if (hdim < PetscAbsInt(formDegree)) break; 2203 for (p = pStratStart[hd]; p < pStratEnd[hd]; p++) { 2204 PetscInt suppSize, s; 2205 const PetscInt *supp; 2206 2207 ierr = DMPlexGetSupportSize(dm, p, &suppSize);CHKERRQ(ierr); 2208 ierr = DMPlexGetSupport(dm, p, &supp);CHKERRQ(ierr); 2209 for (s = 0; s < suppSize; s++) { 2210 DM qdm; 2211 PetscDualSpace qsp, psp; 2212 PetscInt c, coneSize, q; 2213 const PetscInt *cone; 2214 const PetscInt *refCone; 2215 2216 q = supp[0]; 2217 qsp = sp->pointSpaces[q]; 2218 ierr = DMPlexGetConeSize(dm, q, &coneSize);CHKERRQ(ierr); 2219 ierr = DMPlexGetCone(dm, q, &cone);CHKERRQ(ierr); 2220 for (c = 0; c < coneSize; c++) if (cone[c] == p) break; 2221 PetscCheckFalse(c == coneSize,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "cone/support mismatch"); 2222 ierr = PetscDualSpaceGetDM(qsp, &qdm);CHKERRQ(ierr); 2223 ierr = DMPlexGetCone(qdm, 0, &refCone);CHKERRQ(ierr); 2224 /* get the equivalent dual space from the support dual space */ 2225 ierr = PetscDualSpaceGetPointSubspace(qsp, refCone[c], &psp);CHKERRQ(ierr); 2226 if (!s) { 2227 ierr = PetscObjectReference((PetscObject)psp);CHKERRQ(ierr); 2228 sp->pointSpaces[p] = psp; 2229 } 2230 } 2231 } 2232 } 2233 for (p = 1; p < pEnd; p++) { 2234 PetscInt pspdim; 2235 if (!sp->pointSpaces[p]) continue; 2236 ierr = PetscDualSpaceGetInteriorDimension(sp->pointSpaces[p], &pspdim);CHKERRQ(ierr); 2237 ierr = PetscSectionSetDof(section, p, pspdim);CHKERRQ(ierr); 2238 } 2239 } 2240 2241 if (Ncopies > 1) { 2242 Mat intMatScalar, allMatScalar; 2243 PetscDualSpace scalarsp; 2244 PetscDualSpace_Lag *scalarlag; 2245 2246 ierr = PetscDualSpaceDuplicate(sp, &scalarsp);CHKERRQ(ierr); 2247 /* Setting the number of components to Nk is a space with 1 copy of each k-form */ 2248 ierr = PetscDualSpaceSetNumComponents(scalarsp, Nk);CHKERRQ(ierr); 2249 ierr = PetscDualSpaceSetUp(scalarsp);CHKERRQ(ierr); 2250 ierr = PetscDualSpaceGetInteriorData(scalarsp, &(sp->intNodes), &intMatScalar);CHKERRQ(ierr); 2251 ierr = PetscObjectReference((PetscObject)(sp->intNodes));CHKERRQ(ierr); 2252 if (intMatScalar) {ierr = PetscDualSpaceLagrangeMatrixCreateCopies(intMatScalar, Nk, Ncopies, &(sp->intMat));CHKERRQ(ierr);} 2253 ierr = PetscDualSpaceGetAllData(scalarsp, &(sp->allNodes), &allMatScalar);CHKERRQ(ierr); 2254 ierr = PetscObjectReference((PetscObject)(sp->allNodes));CHKERRQ(ierr); 2255 ierr = PetscDualSpaceLagrangeMatrixCreateCopies(allMatScalar, Nk, Ncopies, &(sp->allMat));CHKERRQ(ierr); 2256 sp->spdim = scalarsp->spdim * Ncopies; 2257 sp->spintdim = scalarsp->spintdim * Ncopies; 2258 scalarlag = (PetscDualSpace_Lag *) scalarsp->data; 2259 ierr = PetscLagNodeIndicesReference(scalarlag->vertIndices);CHKERRQ(ierr); 2260 lag->vertIndices = scalarlag->vertIndices; 2261 ierr = PetscLagNodeIndicesReference(scalarlag->intNodeIndices);CHKERRQ(ierr); 2262 lag->intNodeIndices = scalarlag->intNodeIndices; 2263 ierr = PetscLagNodeIndicesReference(scalarlag->allNodeIndices);CHKERRQ(ierr); 2264 lag->allNodeIndices = scalarlag->allNodeIndices; 2265 ierr = PetscDualSpaceDestroy(&scalarsp);CHKERRQ(ierr); 2266 ierr = PetscSectionSetDof(section, 0, sp->spintdim);CHKERRQ(ierr); 2267 ierr = PetscDualSpaceSectionSetUp_Internal(sp, section);CHKERRQ(ierr); 2268 ierr = PetscDualSpaceComputeFunctionalsFromAllData(sp);CHKERRQ(ierr); 2269 ierr = PetscFree2(pStratStart, pStratEnd);CHKERRQ(ierr); 2270 ierr = DMDestroy(&dmint);CHKERRQ(ierr); 2271 PetscFunctionReturn(0); 2272 } 2273 2274 if (trimmed && !continuous) { 2275 /* the dofs of a trimmed space don't have a nice tensor/lattice structure: 2276 * just construct the continuous dual space and copy all of the data over, 2277 * allocating it all to the cell instead of splitting it up between the boundaries */ 2278 PetscDualSpace spcont; 2279 PetscInt spdim, f; 2280 PetscQuadrature allNodes; 2281 PetscDualSpace_Lag *lagc; 2282 Mat allMat; 2283 2284 ierr = PetscDualSpaceDuplicate(sp, &spcont);CHKERRQ(ierr); 2285 ierr = PetscDualSpaceLagrangeSetContinuity(spcont, PETSC_TRUE);CHKERRQ(ierr); 2286 ierr = PetscDualSpaceSetUp(spcont);CHKERRQ(ierr); 2287 ierr = PetscDualSpaceGetDimension(spcont, &spdim);CHKERRQ(ierr); 2288 sp->spdim = sp->spintdim = spdim; 2289 ierr = PetscSectionSetDof(section, 0, spdim);CHKERRQ(ierr); 2290 ierr = PetscDualSpaceSectionSetUp_Internal(sp, section);CHKERRQ(ierr); 2291 ierr = PetscMalloc1(spdim, &(sp->functional));CHKERRQ(ierr); 2292 for (f = 0; f < spdim; f++) { 2293 PetscQuadrature fn; 2294 2295 ierr = PetscDualSpaceGetFunctional(spcont, f, &fn);CHKERRQ(ierr); 2296 ierr = PetscObjectReference((PetscObject)fn);CHKERRQ(ierr); 2297 sp->functional[f] = fn; 2298 } 2299 ierr = PetscDualSpaceGetAllData(spcont, &allNodes, &allMat);CHKERRQ(ierr); 2300 ierr = PetscObjectReference((PetscObject) allNodes);CHKERRQ(ierr); 2301 ierr = PetscObjectReference((PetscObject) allNodes);CHKERRQ(ierr); 2302 sp->allNodes = sp->intNodes = allNodes; 2303 ierr = PetscObjectReference((PetscObject) allMat);CHKERRQ(ierr); 2304 ierr = PetscObjectReference((PetscObject) allMat);CHKERRQ(ierr); 2305 sp->allMat = sp->intMat = allMat; 2306 lagc = (PetscDualSpace_Lag *) spcont->data; 2307 ierr = PetscLagNodeIndicesReference(lagc->vertIndices);CHKERRQ(ierr); 2308 lag->vertIndices = lagc->vertIndices; 2309 ierr = PetscLagNodeIndicesReference(lagc->allNodeIndices);CHKERRQ(ierr); 2310 ierr = PetscLagNodeIndicesReference(lagc->allNodeIndices);CHKERRQ(ierr); 2311 lag->intNodeIndices = lagc->allNodeIndices; 2312 lag->allNodeIndices = lagc->allNodeIndices; 2313 ierr = PetscDualSpaceDestroy(&spcont);CHKERRQ(ierr); 2314 ierr = PetscFree2(pStratStart, pStratEnd);CHKERRQ(ierr); 2315 ierr = DMDestroy(&dmint);CHKERRQ(ierr); 2316 PetscFunctionReturn(0); 2317 } 2318 2319 /* step 3: construct intNodes, and intMat, and combine it with boundray data to make allNodes and allMat */ 2320 if (!tensorSpace) { 2321 if (!tensorCell) {ierr = PetscLagNodeIndicesCreateSimplexVertices(dm, &(lag->vertIndices));CHKERRQ(ierr);} 2322 2323 if (trimmed) { 2324 /* there is one dof in the interior of the a trimmed element for each full polynomial of with degree at most 2325 * order + k - dim - 1 */ 2326 if (order + PetscAbsInt(formDegree) > dim) { 2327 PetscInt sum = order + PetscAbsInt(formDegree) - dim - 1; 2328 PetscInt nDofs; 2329 2330 ierr = PetscDualSpaceLagrangeCreateSimplexNodeMat(nodeFamily, dim, sum, Nk, numNodeSkip, &sp->intNodes, &sp->intMat, &(lag->intNodeIndices));CHKERRQ(ierr); 2331 ierr = MatGetSize(sp->intMat, &nDofs, NULL);CHKERRQ(ierr); 2332 ierr = PetscSectionSetDof(section, 0, nDofs);CHKERRQ(ierr); 2333 } 2334 ierr = PetscDualSpaceSectionSetUp_Internal(sp, section);CHKERRQ(ierr); 2335 ierr = PetscDualSpaceCreateAllDataFromInteriorData(sp);CHKERRQ(ierr); 2336 ierr = PetscDualSpaceLagrangeCreateAllNodeIdx(sp);CHKERRQ(ierr); 2337 } else { 2338 if (!continuous) { 2339 /* if discontinuous just construct one node for each set of dofs (a set of dofs is a basis for the k-form 2340 * space) */ 2341 PetscInt sum = order; 2342 PetscInt nDofs; 2343 2344 ierr = PetscDualSpaceLagrangeCreateSimplexNodeMat(nodeFamily, dim, sum, Nk, numNodeSkip, &sp->intNodes, &sp->intMat, &(lag->intNodeIndices));CHKERRQ(ierr); 2345 ierr = MatGetSize(sp->intMat, &nDofs, NULL);CHKERRQ(ierr); 2346 ierr = PetscSectionSetDof(section, 0, nDofs);CHKERRQ(ierr); 2347 ierr = PetscDualSpaceSectionSetUp_Internal(sp, section);CHKERRQ(ierr); 2348 ierr = PetscObjectReference((PetscObject)(sp->intNodes));CHKERRQ(ierr); 2349 sp->allNodes = sp->intNodes; 2350 ierr = PetscObjectReference((PetscObject)(sp->intMat));CHKERRQ(ierr); 2351 sp->allMat = sp->intMat; 2352 ierr = PetscLagNodeIndicesReference(lag->intNodeIndices);CHKERRQ(ierr); 2353 lag->allNodeIndices = lag->intNodeIndices; 2354 } else { 2355 /* there is one dof in the interior of the a full element for each trimmed polynomial of with degree at most 2356 * order + k - dim, but with complementary form degree */ 2357 if (order + PetscAbsInt(formDegree) > dim) { 2358 PetscDualSpace trimmedsp; 2359 PetscDualSpace_Lag *trimmedlag; 2360 PetscQuadrature intNodes; 2361 PetscInt trFormDegree = formDegree >= 0 ? formDegree - dim : dim - PetscAbsInt(formDegree); 2362 PetscInt nDofs; 2363 Mat intMat; 2364 2365 ierr = PetscDualSpaceDuplicate(sp, &trimmedsp);CHKERRQ(ierr); 2366 ierr = PetscDualSpaceLagrangeSetTrimmed(trimmedsp, PETSC_TRUE);CHKERRQ(ierr); 2367 ierr = PetscDualSpaceSetOrder(trimmedsp, order + PetscAbsInt(formDegree) - dim);CHKERRQ(ierr); 2368 ierr = PetscDualSpaceSetFormDegree(trimmedsp, trFormDegree);CHKERRQ(ierr); 2369 trimmedlag = (PetscDualSpace_Lag *) trimmedsp->data; 2370 trimmedlag->numNodeSkip = numNodeSkip + 1; 2371 ierr = PetscDualSpaceSetUp(trimmedsp);CHKERRQ(ierr); 2372 ierr = PetscDualSpaceGetAllData(trimmedsp, &intNodes, &intMat);CHKERRQ(ierr); 2373 ierr = PetscObjectReference((PetscObject)intNodes);CHKERRQ(ierr); 2374 sp->intNodes = intNodes; 2375 ierr = PetscLagNodeIndicesReference(trimmedlag->allNodeIndices);CHKERRQ(ierr); 2376 lag->intNodeIndices = trimmedlag->allNodeIndices; 2377 ierr = PetscObjectReference((PetscObject)intMat);CHKERRQ(ierr); 2378 if (PetscAbsInt(formDegree) > 0 && PetscAbsInt(formDegree) < dim) { 2379 PetscReal *T; 2380 PetscScalar *work; 2381 PetscInt nCols, nRows; 2382 Mat intMatT; 2383 2384 ierr = MatDuplicate(intMat, MAT_COPY_VALUES, &intMatT);CHKERRQ(ierr); 2385 ierr = MatGetSize(intMat, &nRows, &nCols);CHKERRQ(ierr); 2386 ierr = PetscMalloc2(Nk * Nk, &T, nCols, &work);CHKERRQ(ierr); 2387 ierr = BiunitSimplexSymmetricFormTransformation(dim, formDegree, T);CHKERRQ(ierr); 2388 for (PetscInt row = 0; row < nRows; row++) { 2389 PetscInt nrCols; 2390 const PetscInt *rCols; 2391 const PetscScalar *rVals; 2392 2393 ierr = MatGetRow(intMat, row, &nrCols, &rCols, &rVals);CHKERRQ(ierr); 2394 PetscCheckFalse(nrCols % Nk,PETSC_COMM_SELF, PETSC_ERR_PLIB, "nonzeros in intMat matrix are not in k-form size blocks"); 2395 for (PetscInt b = 0; b < nrCols; b += Nk) { 2396 const PetscScalar *v = &rVals[b]; 2397 PetscScalar *w = &work[b]; 2398 for (PetscInt j = 0; j < Nk; j++) { 2399 w[j] = 0.; 2400 for (PetscInt i = 0; i < Nk; i++) { 2401 w[j] += v[i] * T[i * Nk + j]; 2402 } 2403 } 2404 } 2405 ierr = MatSetValuesBlocked(intMatT, 1, &row, nrCols, rCols, work, INSERT_VALUES);CHKERRQ(ierr); 2406 ierr = MatRestoreRow(intMat, row, &nrCols, &rCols, &rVals);CHKERRQ(ierr); 2407 } 2408 ierr = MatAssemblyBegin(intMatT, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2409 ierr = MatAssemblyEnd(intMatT, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2410 ierr = MatDestroy(&intMat);CHKERRQ(ierr); 2411 intMat = intMatT; 2412 ierr = PetscLagNodeIndicesDestroy(&(lag->intNodeIndices));CHKERRQ(ierr); 2413 ierr = PetscLagNodeIndicesDuplicate(trimmedlag->allNodeIndices, &(lag->intNodeIndices));CHKERRQ(ierr); 2414 { 2415 PetscInt nNodes = lag->intNodeIndices->nNodes; 2416 PetscReal *newNodeVec = lag->intNodeIndices->nodeVec; 2417 const PetscReal *oldNodeVec = trimmedlag->allNodeIndices->nodeVec; 2418 2419 for (PetscInt n = 0; n < nNodes; n++) { 2420 PetscReal *w = &newNodeVec[n * Nk]; 2421 const PetscReal *v = &oldNodeVec[n * Nk]; 2422 2423 for (PetscInt j = 0; j < Nk; j++) { 2424 w[j] = 0.; 2425 for (PetscInt i = 0; i < Nk; i++) { 2426 w[j] += v[i] * T[i * Nk + j]; 2427 } 2428 } 2429 } 2430 } 2431 ierr = PetscFree2(T, work);CHKERRQ(ierr); 2432 } 2433 sp->intMat = intMat; 2434 ierr = MatGetSize(sp->intMat, &nDofs, NULL);CHKERRQ(ierr); 2435 ierr = PetscDualSpaceDestroy(&trimmedsp);CHKERRQ(ierr); 2436 ierr = PetscSectionSetDof(section, 0, nDofs);CHKERRQ(ierr); 2437 } 2438 ierr = PetscDualSpaceSectionSetUp_Internal(sp, section);CHKERRQ(ierr); 2439 ierr = PetscDualSpaceCreateAllDataFromInteriorData(sp);CHKERRQ(ierr); 2440 ierr = PetscDualSpaceLagrangeCreateAllNodeIdx(sp);CHKERRQ(ierr); 2441 } 2442 } 2443 } else { 2444 PetscQuadrature intNodesTrace = NULL; 2445 PetscQuadrature intNodesFiber = NULL; 2446 PetscQuadrature intNodes = NULL; 2447 PetscLagNodeIndices intNodeIndices = NULL; 2448 Mat intMat = NULL; 2449 2450 if (PetscAbsInt(formDegree) < dim) { /* get the trace k-forms on the first facet, and the 0-forms on the edge, 2451 and wedge them together to create some of the k-form dofs */ 2452 PetscDualSpace trace, fiber; 2453 PetscDualSpace_Lag *tracel, *fiberl; 2454 Mat intMatTrace, intMatFiber; 2455 2456 if (sp->pointSpaces[tensorf]) { 2457 ierr = PetscObjectReference((PetscObject)(sp->pointSpaces[tensorf]));CHKERRQ(ierr); 2458 trace = sp->pointSpaces[tensorf]; 2459 } else { 2460 ierr = PetscDualSpaceCreateFacetSubspace_Lagrange(sp,NULL,tensorf,formDegree,Ncopies,PETSC_TRUE,&trace);CHKERRQ(ierr); 2461 } 2462 ierr = PetscDualSpaceCreateEdgeSubspace_Lagrange(sp,order,0,1,PETSC_TRUE,&fiber);CHKERRQ(ierr); 2463 tracel = (PetscDualSpace_Lag *) trace->data; 2464 fiberl = (PetscDualSpace_Lag *) fiber->data; 2465 ierr = PetscLagNodeIndicesCreateTensorVertices(dm, tracel->vertIndices, &(lag->vertIndices));CHKERRQ(ierr); 2466 ierr = PetscDualSpaceGetInteriorData(trace, &intNodesTrace, &intMatTrace);CHKERRQ(ierr); 2467 ierr = PetscDualSpaceGetInteriorData(fiber, &intNodesFiber, &intMatFiber);CHKERRQ(ierr); 2468 if (intNodesTrace && intNodesFiber) { 2469 ierr = PetscQuadratureCreateTensor(intNodesTrace, intNodesFiber, &intNodes);CHKERRQ(ierr); 2470 ierr = MatTensorAltV(intMatTrace, intMatFiber, dim-1, formDegree, 1, 0, &intMat);CHKERRQ(ierr); 2471 ierr = PetscLagNodeIndicesTensor(tracel->intNodeIndices, dim - 1, formDegree, fiberl->intNodeIndices, 1, 0, &intNodeIndices);CHKERRQ(ierr); 2472 } 2473 ierr = PetscObjectReference((PetscObject) intNodesTrace);CHKERRQ(ierr); 2474 ierr = PetscObjectReference((PetscObject) intNodesFiber);CHKERRQ(ierr); 2475 ierr = PetscDualSpaceDestroy(&fiber);CHKERRQ(ierr); 2476 ierr = PetscDualSpaceDestroy(&trace);CHKERRQ(ierr); 2477 } 2478 if (PetscAbsInt(formDegree) > 0) { /* get the trace (k-1)-forms on the first facet, and the 1-forms on the edge, 2479 and wedge them together to create the remaining k-form dofs */ 2480 PetscDualSpace trace, fiber; 2481 PetscDualSpace_Lag *tracel, *fiberl; 2482 PetscQuadrature intNodesTrace2, intNodesFiber2, intNodes2; 2483 PetscLagNodeIndices intNodeIndices2; 2484 Mat intMatTrace, intMatFiber, intMat2; 2485 PetscInt traceDegree = formDegree > 0 ? formDegree - 1 : formDegree + 1; 2486 PetscInt fiberDegree = formDegree > 0 ? 1 : -1; 2487 2488 ierr = PetscDualSpaceCreateFacetSubspace_Lagrange(sp,NULL,tensorf,traceDegree,Ncopies,PETSC_TRUE,&trace);CHKERRQ(ierr); 2489 ierr = PetscDualSpaceCreateEdgeSubspace_Lagrange(sp,order,fiberDegree,1,PETSC_TRUE,&fiber);CHKERRQ(ierr); 2490 tracel = (PetscDualSpace_Lag *) trace->data; 2491 fiberl = (PetscDualSpace_Lag *) fiber->data; 2492 if (!lag->vertIndices) { 2493 ierr = PetscLagNodeIndicesCreateTensorVertices(dm, tracel->vertIndices, &(lag->vertIndices));CHKERRQ(ierr); 2494 } 2495 ierr = PetscDualSpaceGetInteriorData(trace, &intNodesTrace2, &intMatTrace);CHKERRQ(ierr); 2496 ierr = PetscDualSpaceGetInteriorData(fiber, &intNodesFiber2, &intMatFiber);CHKERRQ(ierr); 2497 if (intNodesTrace2 && intNodesFiber2) { 2498 ierr = PetscQuadratureCreateTensor(intNodesTrace2, intNodesFiber2, &intNodes2);CHKERRQ(ierr); 2499 ierr = MatTensorAltV(intMatTrace, intMatFiber, dim-1, traceDegree, 1, fiberDegree, &intMat2);CHKERRQ(ierr); 2500 ierr = PetscLagNodeIndicesTensor(tracel->intNodeIndices, dim - 1, traceDegree, fiberl->intNodeIndices, 1, fiberDegree, &intNodeIndices2);CHKERRQ(ierr); 2501 if (!intMat) { 2502 intMat = intMat2; 2503 intNodes = intNodes2; 2504 intNodeIndices = intNodeIndices2; 2505 } else { 2506 /* merge the matrices, quadrature points, and nodes */ 2507 PetscInt nM; 2508 PetscInt nDof, nDof2; 2509 PetscInt *toMerged = NULL, *toMerged2 = NULL; 2510 PetscQuadrature merged = NULL; 2511 PetscLagNodeIndices intNodeIndicesMerged = NULL; 2512 Mat matMerged = NULL; 2513 2514 ierr = MatGetSize(intMat, &nDof, NULL);CHKERRQ(ierr); 2515 ierr = MatGetSize(intMat2, &nDof2, NULL);CHKERRQ(ierr); 2516 ierr = PetscQuadraturePointsMerge(intNodes, intNodes2, &merged, &toMerged, &toMerged2);CHKERRQ(ierr); 2517 ierr = PetscQuadratureGetData(merged, NULL, NULL, &nM, NULL, NULL);CHKERRQ(ierr); 2518 ierr = MatricesMerge(intMat, intMat2, dim, formDegree, nM, toMerged, toMerged2, &matMerged);CHKERRQ(ierr); 2519 ierr = PetscLagNodeIndicesMerge(intNodeIndices, intNodeIndices2, &intNodeIndicesMerged);CHKERRQ(ierr); 2520 ierr = PetscFree(toMerged);CHKERRQ(ierr); 2521 ierr = PetscFree(toMerged2);CHKERRQ(ierr); 2522 ierr = MatDestroy(&intMat);CHKERRQ(ierr); 2523 ierr = MatDestroy(&intMat2);CHKERRQ(ierr); 2524 ierr = PetscQuadratureDestroy(&intNodes);CHKERRQ(ierr); 2525 ierr = PetscQuadratureDestroy(&intNodes2);CHKERRQ(ierr); 2526 ierr = PetscLagNodeIndicesDestroy(&intNodeIndices);CHKERRQ(ierr); 2527 ierr = PetscLagNodeIndicesDestroy(&intNodeIndices2);CHKERRQ(ierr); 2528 intNodes = merged; 2529 intMat = matMerged; 2530 intNodeIndices = intNodeIndicesMerged; 2531 if (!trimmed) { 2532 /* I think users expect that, when a node has a full basis for the k-forms, 2533 * they should be consecutive dofs. That isn't the case for trimmed spaces, 2534 * but is for some of the nodes in untrimmed spaces, so in that case we 2535 * sort them to group them by node */ 2536 Mat intMatPerm; 2537 2538 ierr = MatPermuteByNodeIdx(intMat, intNodeIndices, &intMatPerm);CHKERRQ(ierr); 2539 ierr = MatDestroy(&intMat);CHKERRQ(ierr); 2540 intMat = intMatPerm; 2541 } 2542 } 2543 } 2544 ierr = PetscDualSpaceDestroy(&fiber);CHKERRQ(ierr); 2545 ierr = PetscDualSpaceDestroy(&trace);CHKERRQ(ierr); 2546 } 2547 ierr = PetscQuadratureDestroy(&intNodesTrace);CHKERRQ(ierr); 2548 ierr = PetscQuadratureDestroy(&intNodesFiber);CHKERRQ(ierr); 2549 sp->intNodes = intNodes; 2550 sp->intMat = intMat; 2551 lag->intNodeIndices = intNodeIndices; 2552 { 2553 PetscInt nDofs = 0; 2554 2555 if (intMat) { 2556 ierr = MatGetSize(intMat, &nDofs, NULL);CHKERRQ(ierr); 2557 } 2558 ierr = PetscSectionSetDof(section, 0, nDofs);CHKERRQ(ierr); 2559 } 2560 ierr = PetscDualSpaceSectionSetUp_Internal(sp, section);CHKERRQ(ierr); 2561 if (continuous) { 2562 ierr = PetscDualSpaceCreateAllDataFromInteriorData(sp);CHKERRQ(ierr); 2563 ierr = PetscDualSpaceLagrangeCreateAllNodeIdx(sp);CHKERRQ(ierr); 2564 } else { 2565 ierr = PetscObjectReference((PetscObject) intNodes);CHKERRQ(ierr); 2566 sp->allNodes = intNodes; 2567 ierr = PetscObjectReference((PetscObject) intMat);CHKERRQ(ierr); 2568 sp->allMat = intMat; 2569 ierr = PetscLagNodeIndicesReference(intNodeIndices);CHKERRQ(ierr); 2570 lag->allNodeIndices = intNodeIndices; 2571 } 2572 } 2573 ierr = PetscSectionGetStorageSize(section, &sp->spdim);CHKERRQ(ierr); 2574 ierr = PetscSectionGetConstrainedStorageSize(section, &sp->spintdim);CHKERRQ(ierr); 2575 ierr = PetscDualSpaceComputeFunctionalsFromAllData(sp);CHKERRQ(ierr); 2576 ierr = PetscFree2(pStratStart, pStratEnd);CHKERRQ(ierr); 2577 ierr = DMDestroy(&dmint);CHKERRQ(ierr); 2578 PetscFunctionReturn(0); 2579 } 2580 2581 /* Create a matrix that represents the transformation that DMPlexVecGetClosure() would need 2582 * to get the representation of the dofs for a mesh point if the mesh point had this orientation 2583 * relative to the cell */ 2584 PetscErrorCode PetscDualSpaceCreateInteriorSymmetryMatrix_Lagrange(PetscDualSpace sp, PetscInt ornt, Mat *symMat) 2585 { 2586 PetscDualSpace_Lag *lag; 2587 DM dm; 2588 PetscLagNodeIndices vertIndices, intNodeIndices; 2589 PetscLagNodeIndices ni; 2590 PetscInt nodeIdxDim, nodeVecDim, nNodes; 2591 PetscInt formDegree; 2592 PetscInt *perm, *permOrnt; 2593 PetscInt *nnz; 2594 PetscInt n; 2595 PetscInt maxGroupSize; 2596 PetscScalar *V, *W, *work; 2597 Mat A; 2598 PetscErrorCode ierr; 2599 2600 PetscFunctionBegin; 2601 if (!sp->spintdim) { 2602 *symMat = NULL; 2603 PetscFunctionReturn(0); 2604 } 2605 lag = (PetscDualSpace_Lag *) sp->data; 2606 vertIndices = lag->vertIndices; 2607 intNodeIndices = lag->intNodeIndices; 2608 ierr = PetscDualSpaceGetDM(sp, &dm);CHKERRQ(ierr); 2609 ierr = PetscDualSpaceGetFormDegree(sp, &formDegree);CHKERRQ(ierr); 2610 ierr = PetscNew(&ni);CHKERRQ(ierr); 2611 ni->refct = 1; 2612 ni->nodeIdxDim = nodeIdxDim = intNodeIndices->nodeIdxDim; 2613 ni->nodeVecDim = nodeVecDim = intNodeIndices->nodeVecDim; 2614 ni->nNodes = nNodes = intNodeIndices->nNodes; 2615 ierr = PetscMalloc1(nNodes * nodeIdxDim, &(ni->nodeIdx));CHKERRQ(ierr); 2616 ierr = PetscMalloc1(nNodes * nodeVecDim, &(ni->nodeVec));CHKERRQ(ierr); 2617 /* push forward the dofs by the symmetry of the reference element induced by ornt */ 2618 ierr = PetscLagNodeIndicesPushForward(dm, vertIndices, 0, vertIndices, intNodeIndices, ornt, formDegree, ni->nodeIdx, ni->nodeVec);CHKERRQ(ierr); 2619 /* get the revlex order for both the original and transformed dofs */ 2620 ierr = PetscLagNodeIndicesGetPermutation(intNodeIndices, &perm);CHKERRQ(ierr); 2621 ierr = PetscLagNodeIndicesGetPermutation(ni, &permOrnt);CHKERRQ(ierr); 2622 ierr = PetscMalloc1(nNodes, &nnz);CHKERRQ(ierr); 2623 for (n = 0, maxGroupSize = 0; n < nNodes;) { /* incremented in the loop */ 2624 PetscInt *nind = &(ni->nodeIdx[permOrnt[n] * nodeIdxDim]); 2625 PetscInt m, nEnd; 2626 PetscInt groupSize; 2627 /* for each group of dofs that have the same nodeIdx coordinate */ 2628 for (nEnd = n + 1; nEnd < nNodes; nEnd++) { 2629 PetscInt *mind = &(ni->nodeIdx[permOrnt[nEnd] * nodeIdxDim]); 2630 PetscInt d; 2631 2632 /* compare the oriented permutation indices */ 2633 for (d = 0; d < nodeIdxDim; d++) if (mind[d] != nind[d]) break; 2634 if (d < nodeIdxDim) break; 2635 } 2636 /* permOrnt[[n, nEnd)] is a group of dofs that, under the symmetry are at the same location */ 2637 2638 /* the symmetry had better map the group of dofs with the same permuted nodeIdx 2639 * to a group of dofs with the same size, otherwise we messed up */ 2640 if (PetscDefined(USE_DEBUG)) { 2641 PetscInt m; 2642 PetscInt *nind = &(intNodeIndices->nodeIdx[perm[n] * nodeIdxDim]); 2643 2644 for (m = n + 1; m < nEnd; m++) { 2645 PetscInt *mind = &(intNodeIndices->nodeIdx[perm[m] * nodeIdxDim]); 2646 PetscInt d; 2647 2648 /* compare the oriented permutation indices */ 2649 for (d = 0; d < nodeIdxDim; d++) if (mind[d] != nind[d]) break; 2650 if (d < nodeIdxDim) break; 2651 } 2652 PetscCheckFalse(m < nEnd,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Dofs with same index after symmetry not same block size"); 2653 } 2654 groupSize = nEnd - n; 2655 /* each pushforward dof vector will be expressed in a basis of the unpermuted dofs */ 2656 for (m = n; m < nEnd; m++) nnz[permOrnt[m]] = groupSize; 2657 2658 maxGroupSize = PetscMax(maxGroupSize, nEnd - n); 2659 n = nEnd; 2660 } 2661 PetscCheckFalse(maxGroupSize > nodeVecDim,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Dofs are not in blocks that can be solved"); 2662 ierr = MatCreateSeqAIJ(PETSC_COMM_SELF, nNodes, nNodes, 0, nnz, &A);CHKERRQ(ierr); 2663 ierr = PetscFree(nnz);CHKERRQ(ierr); 2664 ierr = PetscMalloc3(maxGroupSize * nodeVecDim, &V, maxGroupSize * nodeVecDim, &W, nodeVecDim * 2, &work);CHKERRQ(ierr); 2665 for (n = 0; n < nNodes;) { /* incremented in the loop */ 2666 PetscInt *nind = &(ni->nodeIdx[permOrnt[n] * nodeIdxDim]); 2667 PetscInt nEnd; 2668 PetscInt m; 2669 PetscInt groupSize; 2670 for (nEnd = n + 1; nEnd < nNodes; nEnd++) { 2671 PetscInt *mind = &(ni->nodeIdx[permOrnt[nEnd] * nodeIdxDim]); 2672 PetscInt d; 2673 2674 /* compare the oriented permutation indices */ 2675 for (d = 0; d < nodeIdxDim; d++) if (mind[d] != nind[d]) break; 2676 if (d < nodeIdxDim) break; 2677 } 2678 groupSize = nEnd - n; 2679 /* get all of the vectors from the original and all of the pushforward vectors */ 2680 for (m = n; m < nEnd; m++) { 2681 PetscInt d; 2682 2683 for (d = 0; d < nodeVecDim; d++) { 2684 V[(m - n) * nodeVecDim + d] = intNodeIndices->nodeVec[perm[m] * nodeVecDim + d]; 2685 W[(m - n) * nodeVecDim + d] = ni->nodeVec[permOrnt[m] * nodeVecDim + d]; 2686 } 2687 } 2688 /* now we have to solve for W in terms of V: the systems isn't always square, but the span 2689 * of V and W should always be the same, so the solution of the normal equations works */ 2690 { 2691 char transpose = 'N'; 2692 PetscBLASInt bm = nodeVecDim; 2693 PetscBLASInt bn = groupSize; 2694 PetscBLASInt bnrhs = groupSize; 2695 PetscBLASInt blda = bm; 2696 PetscBLASInt bldb = bm; 2697 PetscBLASInt blwork = 2 * nodeVecDim; 2698 PetscBLASInt info; 2699 2700 PetscStackCallBLAS("LAPACKgels",LAPACKgels_(&transpose,&bm,&bn,&bnrhs,V,&blda,W,&bldb,work,&blwork, &info)); 2701 PetscCheckFalse(info != 0,PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GELS"); 2702 /* repack */ 2703 { 2704 PetscInt i, j; 2705 2706 for (i = 0; i < groupSize; i++) { 2707 for (j = 0; j < groupSize; j++) { 2708 /* notice the different leading dimension */ 2709 V[i * groupSize + j] = W[i * nodeVecDim + j]; 2710 } 2711 } 2712 } 2713 if (PetscDefined(USE_DEBUG)) { 2714 PetscReal res; 2715 2716 /* check that the normal error is 0 */ 2717 for (m = n; m < nEnd; m++) { 2718 PetscInt d; 2719 2720 for (d = 0; d < nodeVecDim; d++) { 2721 W[(m - n) * nodeVecDim + d] = ni->nodeVec[permOrnt[m] * nodeVecDim + d]; 2722 } 2723 } 2724 res = 0.; 2725 for (PetscInt i = 0; i < groupSize; i++) { 2726 for (PetscInt j = 0; j < nodeVecDim; j++) { 2727 for (PetscInt k = 0; k < groupSize; k++) { 2728 W[i * nodeVecDim + j] -= V[i * groupSize + k] * intNodeIndices->nodeVec[perm[n+k] * nodeVecDim + j]; 2729 } 2730 res += PetscAbsScalar(W[i * nodeVecDim + j]); 2731 } 2732 } 2733 PetscCheckFalse(res > PETSC_SMALL,PETSC_COMM_SELF,PETSC_ERR_LIB,"Dof block did not solve"); 2734 } 2735 } 2736 ierr = MatSetValues(A, groupSize, &permOrnt[n], groupSize, &perm[n], V, INSERT_VALUES);CHKERRQ(ierr); 2737 n = nEnd; 2738 } 2739 ierr = MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2740 ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2741 *symMat = A; 2742 ierr = PetscFree3(V,W,work);CHKERRQ(ierr); 2743 ierr = PetscLagNodeIndicesDestroy(&ni);CHKERRQ(ierr); 2744 PetscFunctionReturn(0); 2745 } 2746 2747 #define BaryIndex(perEdge,a,b,c) (((b)*(2*perEdge+1-(b)))/2)+(c) 2748 2749 #define CartIndex(perEdge,a,b) (perEdge*(a)+b) 2750 2751 /* the existing interface for symmetries is insufficient for all cases: 2752 * - it should be sufficient for form degrees that are scalar (0 and n) 2753 * - it should be sufficient for hypercube dofs 2754 * - it isn't sufficient for simplex cells with non-scalar form degrees if 2755 * there are any dofs in the interior 2756 * 2757 * We compute the general transformation matrices, and if they fit, we return them, 2758 * otherwise we error (but we should probably change the interface to allow for 2759 * these symmetries) 2760 */ 2761 static PetscErrorCode PetscDualSpaceGetSymmetries_Lagrange(PetscDualSpace sp, const PetscInt ****perms, const PetscScalar ****flips) 2762 { 2763 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *) sp->data; 2764 PetscInt dim, order, Nc; 2765 PetscErrorCode ierr; 2766 2767 PetscFunctionBegin; 2768 ierr = PetscDualSpaceGetOrder(sp,&order);CHKERRQ(ierr); 2769 ierr = PetscDualSpaceGetNumComponents(sp,&Nc);CHKERRQ(ierr); 2770 ierr = DMGetDimension(sp->dm,&dim);CHKERRQ(ierr); 2771 if (!lag->symComputed) { /* store symmetries */ 2772 PetscInt pStart, pEnd, p; 2773 PetscInt numPoints; 2774 PetscInt numFaces; 2775 PetscInt spintdim; 2776 PetscInt ***symperms; 2777 PetscScalar ***symflips; 2778 2779 ierr = DMPlexGetChart(sp->dm, &pStart, &pEnd);CHKERRQ(ierr); 2780 numPoints = pEnd - pStart; 2781 { 2782 DMPolytopeType ct; 2783 /* The number of arrangements is no longer based on the number of faces */ 2784 ierr = DMPlexGetCellType(sp->dm, 0, &ct);CHKERRQ(ierr); 2785 numFaces = DMPolytopeTypeGetNumArrangments(ct) / 2; 2786 } 2787 ierr = PetscCalloc1(numPoints,&symperms);CHKERRQ(ierr); 2788 ierr = PetscCalloc1(numPoints,&symflips);CHKERRQ(ierr); 2789 spintdim = sp->spintdim; 2790 /* The nodal symmetry behavior is not present when tensorSpace != tensorCell: someone might want this for the "S" 2791 * family of FEEC spaces. Most used in particular are discontinuous polynomial L2 spaces in tensor cells, where 2792 * the symmetries are not necessary for FE assembly. So for now we assume this is the case and don't return 2793 * symmetries if tensorSpace != tensorCell */ 2794 if (spintdim && 0 < dim && dim < 3 && (lag->tensorSpace == lag->tensorCell)) { /* compute self symmetries */ 2795 PetscInt **cellSymperms; 2796 PetscScalar **cellSymflips; 2797 PetscInt ornt; 2798 PetscInt nCopies = Nc / lag->intNodeIndices->nodeVecDim; 2799 PetscInt nNodes = lag->intNodeIndices->nNodes; 2800 2801 lag->numSelfSym = 2 * numFaces; 2802 lag->selfSymOff = numFaces; 2803 ierr = PetscCalloc1(2*numFaces,&cellSymperms);CHKERRQ(ierr); 2804 ierr = PetscCalloc1(2*numFaces,&cellSymflips);CHKERRQ(ierr); 2805 /* we want to be able to index symmetries directly with the orientations, which range from [-numFaces,numFaces) */ 2806 symperms[0] = &cellSymperms[numFaces]; 2807 symflips[0] = &cellSymflips[numFaces]; 2808 PetscCheckFalse(lag->intNodeIndices->nodeVecDim * nCopies != Nc,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Node indices incompatible with dofs"); 2809 PetscCheckFalse(nNodes * nCopies != spintdim,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Node indices incompatible with dofs"); 2810 for (ornt = -numFaces; ornt < numFaces; ornt++) { /* for every symmetry, compute the symmetry matrix, and extract rows to see if it fits in the perm + flip framework */ 2811 Mat symMat; 2812 PetscInt *perm; 2813 PetscScalar *flips; 2814 PetscInt i; 2815 2816 if (!ornt) continue; 2817 ierr = PetscMalloc1(spintdim, &perm);CHKERRQ(ierr); 2818 ierr = PetscCalloc1(spintdim, &flips);CHKERRQ(ierr); 2819 for (i = 0; i < spintdim; i++) perm[i] = -1; 2820 ierr = PetscDualSpaceCreateInteriorSymmetryMatrix_Lagrange(sp, ornt, &symMat);CHKERRQ(ierr); 2821 for (i = 0; i < nNodes; i++) { 2822 PetscInt ncols; 2823 PetscInt j, k; 2824 const PetscInt *cols; 2825 const PetscScalar *vals; 2826 PetscBool nz_seen = PETSC_FALSE; 2827 2828 ierr = MatGetRow(symMat, i, &ncols, &cols, &vals);CHKERRQ(ierr); 2829 for (j = 0; j < ncols; j++) { 2830 if (PetscAbsScalar(vals[j]) > PETSC_SMALL) { 2831 PetscCheckFalse(nz_seen,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "This dual space has symmetries that can't be described as a permutation + sign flips"); 2832 nz_seen = PETSC_TRUE; 2833 PetscCheckFalse(PetscAbsReal(PetscAbsScalar(vals[j]) - PetscRealConstant(1.)) > PETSC_SMALL,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "This dual space has symmetries that can't be described as a permutation + sign flips"); 2834 PetscCheckFalse(PetscAbsReal(PetscImaginaryPart(vals[j])) > PETSC_SMALL,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "This dual space has symmetries that can't be described as a permutation + sign flips"); 2835 PetscCheckFalse(perm[cols[j] * nCopies] >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "This dual space has symmetries that can't be described as a permutation + sign flips"); 2836 for (k = 0; k < nCopies; k++) { 2837 perm[cols[j] * nCopies + k] = i * nCopies + k; 2838 } 2839 if (PetscRealPart(vals[j]) < 0.) { 2840 for (k = 0; k < nCopies; k++) { 2841 flips[i * nCopies + k] = -1.; 2842 } 2843 } else { 2844 for (k = 0; k < nCopies; k++) { 2845 flips[i * nCopies + k] = 1.; 2846 } 2847 } 2848 } 2849 } 2850 ierr = MatRestoreRow(symMat, i, &ncols, &cols, &vals);CHKERRQ(ierr); 2851 } 2852 ierr = MatDestroy(&symMat);CHKERRQ(ierr); 2853 /* if there were no sign flips, keep NULL */ 2854 for (i = 0; i < spintdim; i++) if (flips[i] != 1.) break; 2855 if (i == spintdim) { 2856 ierr = PetscFree(flips);CHKERRQ(ierr); 2857 flips = NULL; 2858 } 2859 /* if the permutation is identity, keep NULL */ 2860 for (i = 0; i < spintdim; i++) if (perm[i] != i) break; 2861 if (i == spintdim) { 2862 ierr = PetscFree(perm);CHKERRQ(ierr); 2863 perm = NULL; 2864 } 2865 symperms[0][ornt] = perm; 2866 symflips[0][ornt] = flips; 2867 } 2868 /* if no orientations produced non-identity permutations, keep NULL */ 2869 for (ornt = -numFaces; ornt < numFaces; ornt++) if (symperms[0][ornt]) break; 2870 if (ornt == numFaces) { 2871 ierr = PetscFree(cellSymperms);CHKERRQ(ierr); 2872 symperms[0] = NULL; 2873 } 2874 /* if no orientations produced sign flips, keep NULL */ 2875 for (ornt = -numFaces; ornt < numFaces; ornt++) if (symflips[0][ornt]) break; 2876 if (ornt == numFaces) { 2877 ierr = PetscFree(cellSymflips);CHKERRQ(ierr); 2878 symflips[0] = NULL; 2879 } 2880 } 2881 { /* get the symmetries of closure points */ 2882 PetscInt closureSize = 0; 2883 PetscInt *closure = NULL; 2884 PetscInt r; 2885 2886 ierr = DMPlexGetTransitiveClosure(sp->dm,0,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr); 2887 for (r = 0; r < closureSize; r++) { 2888 PetscDualSpace psp; 2889 PetscInt point = closure[2 * r]; 2890 PetscInt pspintdim; 2891 const PetscInt ***psymperms = NULL; 2892 const PetscScalar ***psymflips = NULL; 2893 2894 if (!point) continue; 2895 ierr = PetscDualSpaceGetPointSubspace(sp, point, &psp);CHKERRQ(ierr); 2896 if (!psp) continue; 2897 ierr = PetscDualSpaceGetInteriorDimension(psp, &pspintdim);CHKERRQ(ierr); 2898 if (!pspintdim) continue; 2899 ierr = PetscDualSpaceGetSymmetries(psp,&psymperms,&psymflips);CHKERRQ(ierr); 2900 symperms[r] = (PetscInt **) (psymperms ? psymperms[0] : NULL); 2901 symflips[r] = (PetscScalar **) (psymflips ? psymflips[0] : NULL); 2902 } 2903 ierr = DMPlexRestoreTransitiveClosure(sp->dm,0,PETSC_TRUE,&closureSize,&closure);CHKERRQ(ierr); 2904 } 2905 for (p = 0; p < pEnd; p++) if (symperms[p]) break; 2906 if (p == pEnd) { 2907 ierr = PetscFree(symperms);CHKERRQ(ierr); 2908 symperms = NULL; 2909 } 2910 for (p = 0; p < pEnd; p++) if (symflips[p]) break; 2911 if (p == pEnd) { 2912 ierr = PetscFree(symflips);CHKERRQ(ierr); 2913 symflips = NULL; 2914 } 2915 lag->symperms = symperms; 2916 lag->symflips = symflips; 2917 lag->symComputed = PETSC_TRUE; 2918 } 2919 if (perms) *perms = (const PetscInt ***) lag->symperms; 2920 if (flips) *flips = (const PetscScalar ***) lag->symflips; 2921 PetscFunctionReturn(0); 2922 } 2923 2924 static PetscErrorCode PetscDualSpaceLagrangeGetContinuity_Lagrange(PetscDualSpace sp, PetscBool *continuous) 2925 { 2926 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *) sp->data; 2927 2928 PetscFunctionBegin; 2929 PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1); 2930 PetscValidPointer(continuous, 2); 2931 *continuous = lag->continuous; 2932 PetscFunctionReturn(0); 2933 } 2934 2935 static PetscErrorCode PetscDualSpaceLagrangeSetContinuity_Lagrange(PetscDualSpace sp, PetscBool continuous) 2936 { 2937 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *) sp->data; 2938 2939 PetscFunctionBegin; 2940 PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1); 2941 lag->continuous = continuous; 2942 PetscFunctionReturn(0); 2943 } 2944 2945 /*@ 2946 PetscDualSpaceLagrangeGetContinuity - Retrieves the flag for element continuity 2947 2948 Not Collective 2949 2950 Input Parameter: 2951 . sp - the PetscDualSpace 2952 2953 Output Parameter: 2954 . continuous - flag for element continuity 2955 2956 Level: intermediate 2957 2958 .seealso: PetscDualSpaceLagrangeSetContinuity() 2959 @*/ 2960 PetscErrorCode PetscDualSpaceLagrangeGetContinuity(PetscDualSpace sp, PetscBool *continuous) 2961 { 2962 PetscErrorCode ierr; 2963 2964 PetscFunctionBegin; 2965 PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1); 2966 PetscValidPointer(continuous, 2); 2967 ierr = PetscTryMethod(sp, "PetscDualSpaceLagrangeGetContinuity_C", (PetscDualSpace,PetscBool*),(sp,continuous));CHKERRQ(ierr); 2968 PetscFunctionReturn(0); 2969 } 2970 2971 /*@ 2972 PetscDualSpaceLagrangeSetContinuity - Indicate whether the element is continuous 2973 2974 Logically Collective on sp 2975 2976 Input Parameters: 2977 + sp - the PetscDualSpace 2978 - continuous - flag for element continuity 2979 2980 Options Database: 2981 . -petscdualspace_lagrange_continuity <bool> 2982 2983 Level: intermediate 2984 2985 .seealso: PetscDualSpaceLagrangeGetContinuity() 2986 @*/ 2987 PetscErrorCode PetscDualSpaceLagrangeSetContinuity(PetscDualSpace sp, PetscBool continuous) 2988 { 2989 PetscErrorCode ierr; 2990 2991 PetscFunctionBegin; 2992 PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1); 2993 PetscValidLogicalCollectiveBool(sp, continuous, 2); 2994 ierr = PetscTryMethod(sp, "PetscDualSpaceLagrangeSetContinuity_C", (PetscDualSpace,PetscBool),(sp,continuous));CHKERRQ(ierr); 2995 PetscFunctionReturn(0); 2996 } 2997 2998 static PetscErrorCode PetscDualSpaceLagrangeGetTensor_Lagrange(PetscDualSpace sp, PetscBool *tensor) 2999 { 3000 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *)sp->data; 3001 3002 PetscFunctionBegin; 3003 *tensor = lag->tensorSpace; 3004 PetscFunctionReturn(0); 3005 } 3006 3007 static PetscErrorCode PetscDualSpaceLagrangeSetTensor_Lagrange(PetscDualSpace sp, PetscBool tensor) 3008 { 3009 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *)sp->data; 3010 3011 PetscFunctionBegin; 3012 lag->tensorSpace = tensor; 3013 PetscFunctionReturn(0); 3014 } 3015 3016 static PetscErrorCode PetscDualSpaceLagrangeGetTrimmed_Lagrange(PetscDualSpace sp, PetscBool *trimmed) 3017 { 3018 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *)sp->data; 3019 3020 PetscFunctionBegin; 3021 *trimmed = lag->trimmed; 3022 PetscFunctionReturn(0); 3023 } 3024 3025 static PetscErrorCode PetscDualSpaceLagrangeSetTrimmed_Lagrange(PetscDualSpace sp, PetscBool trimmed) 3026 { 3027 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *)sp->data; 3028 3029 PetscFunctionBegin; 3030 lag->trimmed = trimmed; 3031 PetscFunctionReturn(0); 3032 } 3033 3034 static PetscErrorCode PetscDualSpaceLagrangeGetNodeType_Lagrange(PetscDualSpace sp, PetscDTNodeType *nodeType, PetscBool *boundary, PetscReal *exponent) 3035 { 3036 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *)sp->data; 3037 3038 PetscFunctionBegin; 3039 if (nodeType) *nodeType = lag->nodeType; 3040 if (boundary) *boundary = lag->endNodes; 3041 if (exponent) *exponent = lag->nodeExponent; 3042 PetscFunctionReturn(0); 3043 } 3044 3045 static PetscErrorCode PetscDualSpaceLagrangeSetNodeType_Lagrange(PetscDualSpace sp, PetscDTNodeType nodeType, PetscBool boundary, PetscReal exponent) 3046 { 3047 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *)sp->data; 3048 3049 PetscFunctionBegin; 3050 PetscCheckFalse(nodeType == PETSCDTNODES_GAUSSJACOBI && exponent <= -1.,PetscObjectComm((PetscObject) sp), PETSC_ERR_ARG_OUTOFRANGE, "Exponent must be > -1"); 3051 lag->nodeType = nodeType; 3052 lag->endNodes = boundary; 3053 lag->nodeExponent = exponent; 3054 PetscFunctionReturn(0); 3055 } 3056 3057 static PetscErrorCode PetscDualSpaceLagrangeGetUseMoments_Lagrange(PetscDualSpace sp, PetscBool *useMoments) 3058 { 3059 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *)sp->data; 3060 3061 PetscFunctionBegin; 3062 *useMoments = lag->useMoments; 3063 PetscFunctionReturn(0); 3064 } 3065 3066 static PetscErrorCode PetscDualSpaceLagrangeSetUseMoments_Lagrange(PetscDualSpace sp, PetscBool useMoments) 3067 { 3068 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *)sp->data; 3069 3070 PetscFunctionBegin; 3071 lag->useMoments = useMoments; 3072 PetscFunctionReturn(0); 3073 } 3074 3075 static PetscErrorCode PetscDualSpaceLagrangeGetMomentOrder_Lagrange(PetscDualSpace sp, PetscInt *momentOrder) 3076 { 3077 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *)sp->data; 3078 3079 PetscFunctionBegin; 3080 *momentOrder = lag->momentOrder; 3081 PetscFunctionReturn(0); 3082 } 3083 3084 static PetscErrorCode PetscDualSpaceLagrangeSetMomentOrder_Lagrange(PetscDualSpace sp, PetscInt momentOrder) 3085 { 3086 PetscDualSpace_Lag *lag = (PetscDualSpace_Lag *)sp->data; 3087 3088 PetscFunctionBegin; 3089 lag->momentOrder = momentOrder; 3090 PetscFunctionReturn(0); 3091 } 3092 3093 /*@ 3094 PetscDualSpaceLagrangeGetTensor - Get the tensor nature of the dual space 3095 3096 Not collective 3097 3098 Input Parameter: 3099 . sp - The PetscDualSpace 3100 3101 Output Parameter: 3102 . tensor - Whether the dual space has tensor layout (vs. simplicial) 3103 3104 Level: intermediate 3105 3106 .seealso: PetscDualSpaceLagrangeSetTensor(), PetscDualSpaceCreate() 3107 @*/ 3108 PetscErrorCode PetscDualSpaceLagrangeGetTensor(PetscDualSpace sp, PetscBool *tensor) 3109 { 3110 PetscErrorCode ierr; 3111 3112 PetscFunctionBegin; 3113 PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1); 3114 PetscValidPointer(tensor, 2); 3115 ierr = PetscTryMethod(sp,"PetscDualSpaceLagrangeGetTensor_C",(PetscDualSpace,PetscBool *),(sp,tensor));CHKERRQ(ierr); 3116 PetscFunctionReturn(0); 3117 } 3118 3119 /*@ 3120 PetscDualSpaceLagrangeSetTensor - Set the tensor nature of the dual space 3121 3122 Not collective 3123 3124 Input Parameters: 3125 + sp - The PetscDualSpace 3126 - tensor - Whether the dual space has tensor layout (vs. simplicial) 3127 3128 Level: intermediate 3129 3130 .seealso: PetscDualSpaceLagrangeGetTensor(), PetscDualSpaceCreate() 3131 @*/ 3132 PetscErrorCode PetscDualSpaceLagrangeSetTensor(PetscDualSpace sp, PetscBool tensor) 3133 { 3134 PetscErrorCode ierr; 3135 3136 PetscFunctionBegin; 3137 PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1); 3138 ierr = PetscTryMethod(sp,"PetscDualSpaceLagrangeSetTensor_C",(PetscDualSpace,PetscBool),(sp,tensor));CHKERRQ(ierr); 3139 PetscFunctionReturn(0); 3140 } 3141 3142 /*@ 3143 PetscDualSpaceLagrangeGetTrimmed - Get the trimmed nature of the dual space 3144 3145 Not collective 3146 3147 Input Parameter: 3148 . sp - The PetscDualSpace 3149 3150 Output Parameter: 3151 . trimmed - Whether the dual space represents to dual basis of a trimmed polynomial space (e.g. Raviart-Thomas and higher order / other form degree variants) 3152 3153 Level: intermediate 3154 3155 .seealso: PetscDualSpaceLagrangeSetTrimmed(), PetscDualSpaceCreate() 3156 @*/ 3157 PetscErrorCode PetscDualSpaceLagrangeGetTrimmed(PetscDualSpace sp, PetscBool *trimmed) 3158 { 3159 PetscErrorCode ierr; 3160 3161 PetscFunctionBegin; 3162 PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1); 3163 PetscValidPointer(trimmed, 2); 3164 ierr = PetscTryMethod(sp,"PetscDualSpaceLagrangeGetTrimmed_C",(PetscDualSpace,PetscBool *),(sp,trimmed));CHKERRQ(ierr); 3165 PetscFunctionReturn(0); 3166 } 3167 3168 /*@ 3169 PetscDualSpaceLagrangeSetTrimmed - Set the trimmed nature of the dual space 3170 3171 Not collective 3172 3173 Input Parameters: 3174 + sp - The PetscDualSpace 3175 - trimmed - Whether the dual space represents to dual basis of a trimmed polynomial space (e.g. Raviart-Thomas and higher order / other form degree variants) 3176 3177 Level: intermediate 3178 3179 .seealso: PetscDualSpaceLagrangeGetTrimmed(), PetscDualSpaceCreate() 3180 @*/ 3181 PetscErrorCode PetscDualSpaceLagrangeSetTrimmed(PetscDualSpace sp, PetscBool trimmed) 3182 { 3183 PetscErrorCode ierr; 3184 3185 PetscFunctionBegin; 3186 PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1); 3187 ierr = PetscTryMethod(sp,"PetscDualSpaceLagrangeSetTrimmed_C",(PetscDualSpace,PetscBool),(sp,trimmed));CHKERRQ(ierr); 3188 PetscFunctionReturn(0); 3189 } 3190 3191 /*@ 3192 PetscDualSpaceLagrangeGetNodeType - Get a description of how nodes are laid out for Lagrange polynomials in this 3193 dual space 3194 3195 Not collective 3196 3197 Input Parameter: 3198 . sp - The PetscDualSpace 3199 3200 Output Parameters: 3201 + nodeType - The type of nodes 3202 . boundary - Whether the node type is one that includes endpoints (if nodeType is PETSCDTNODES_GAUSSJACOBI, nodes that 3203 include the boundary are Gauss-Lobatto-Jacobi nodes) 3204 - exponent - If nodeType is PETSCDTNODES_GAUSJACOBI, indicates the exponent used for both ends of the 1D Jacobi weight function 3205 '0' is Gauss-Legendre, '-0.5' is Gauss-Chebyshev of the first type, '0.5' is Gauss-Chebyshev of the second type 3206 3207 Level: advanced 3208 3209 .seealso: PetscDTNodeType, PetscDualSpaceLagrangeSetNodeType() 3210 @*/ 3211 PetscErrorCode PetscDualSpaceLagrangeGetNodeType(PetscDualSpace sp, PetscDTNodeType *nodeType, PetscBool *boundary, PetscReal *exponent) 3212 { 3213 PetscErrorCode ierr; 3214 3215 PetscFunctionBegin; 3216 PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1); 3217 if (nodeType) PetscValidPointer(nodeType, 2); 3218 if (boundary) PetscValidPointer(boundary, 3); 3219 if (exponent) PetscValidPointer(exponent, 4); 3220 ierr = PetscTryMethod(sp,"PetscDualSpaceLagrangeGetNodeType_C",(PetscDualSpace,PetscDTNodeType *,PetscBool *,PetscReal *),(sp,nodeType,boundary,exponent));CHKERRQ(ierr); 3221 PetscFunctionReturn(0); 3222 } 3223 3224 /*@ 3225 PetscDualSpaceLagrangeSetNodeType - Set a description of how nodes are laid out for Lagrange polynomials in this 3226 dual space 3227 3228 Logically collective 3229 3230 Input Parameters: 3231 + sp - The PetscDualSpace 3232 . nodeType - The type of nodes 3233 . boundary - Whether the node type is one that includes endpoints (if nodeType is PETSCDTNODES_GAUSSJACOBI, nodes that 3234 include the boundary are Gauss-Lobatto-Jacobi nodes) 3235 - exponent - If nodeType is PETSCDTNODES_GAUSJACOBI, indicates the exponent used for both ends of the 1D Jacobi weight function 3236 '0' is Gauss-Legendre, '-0.5' is Gauss-Chebyshev of the first type, '0.5' is Gauss-Chebyshev of the second type 3237 3238 Level: advanced 3239 3240 .seealso: PetscDTNodeType, PetscDualSpaceLagrangeGetNodeType() 3241 @*/ 3242 PetscErrorCode PetscDualSpaceLagrangeSetNodeType(PetscDualSpace sp, PetscDTNodeType nodeType, PetscBool boundary, PetscReal exponent) 3243 { 3244 PetscErrorCode ierr; 3245 3246 PetscFunctionBegin; 3247 PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1); 3248 ierr = PetscTryMethod(sp,"PetscDualSpaceLagrangeSetNodeType_C",(PetscDualSpace,PetscDTNodeType,PetscBool,PetscReal),(sp,nodeType,boundary,exponent));CHKERRQ(ierr); 3249 PetscFunctionReturn(0); 3250 } 3251 3252 /*@ 3253 PetscDualSpaceLagrangeGetUseMoments - Get the flag for using moment functionals 3254 3255 Not collective 3256 3257 Input Parameter: 3258 . sp - The PetscDualSpace 3259 3260 Output Parameter: 3261 . useMoments - Moment flag 3262 3263 Level: advanced 3264 3265 .seealso: PetscDualSpaceLagrangeSetUseMoments() 3266 @*/ 3267 PetscErrorCode PetscDualSpaceLagrangeGetUseMoments(PetscDualSpace sp, PetscBool *useMoments) 3268 { 3269 PetscErrorCode ierr; 3270 3271 PetscFunctionBegin; 3272 PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1); 3273 PetscValidBoolPointer(useMoments, 2); 3274 ierr = PetscUseMethod(sp,"PetscDualSpaceLagrangeGetUseMoments_C",(PetscDualSpace,PetscBool *),(sp,useMoments));CHKERRQ(ierr); 3275 PetscFunctionReturn(0); 3276 } 3277 3278 /*@ 3279 PetscDualSpaceLagrangeSetUseMoments - Set the flag for moment functionals 3280 3281 Logically collective 3282 3283 Input Parameters: 3284 + sp - The PetscDualSpace 3285 - useMoments - The flag for moment functionals 3286 3287 Level: advanced 3288 3289 .seealso: PetscDualSpaceLagrangeGetUseMoments() 3290 @*/ 3291 PetscErrorCode PetscDualSpaceLagrangeSetUseMoments(PetscDualSpace sp, PetscBool useMoments) 3292 { 3293 PetscErrorCode ierr; 3294 3295 PetscFunctionBegin; 3296 PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1); 3297 ierr = PetscTryMethod(sp,"PetscDualSpaceLagrangeSetUseMoments_C",(PetscDualSpace,PetscBool),(sp,useMoments));CHKERRQ(ierr); 3298 PetscFunctionReturn(0); 3299 } 3300 3301 /*@ 3302 PetscDualSpaceLagrangeGetMomentOrder - Get the order for moment integration 3303 3304 Not collective 3305 3306 Input Parameter: 3307 . sp - The PetscDualSpace 3308 3309 Output Parameter: 3310 . order - Moment integration order 3311 3312 Level: advanced 3313 3314 .seealso: PetscDualSpaceLagrangeSetMomentOrder() 3315 @*/ 3316 PetscErrorCode PetscDualSpaceLagrangeGetMomentOrder(PetscDualSpace sp, PetscInt *order) 3317 { 3318 PetscErrorCode ierr; 3319 3320 PetscFunctionBegin; 3321 PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1); 3322 PetscValidIntPointer(order, 2); 3323 ierr = PetscUseMethod(sp,"PetscDualSpaceLagrangeGetMomentOrder_C",(PetscDualSpace,PetscInt *),(sp,order));CHKERRQ(ierr); 3324 PetscFunctionReturn(0); 3325 } 3326 3327 /*@ 3328 PetscDualSpaceLagrangeSetMomentOrder - Set the order for moment integration 3329 3330 Logically collective 3331 3332 Input Parameters: 3333 + sp - The PetscDualSpace 3334 - order - The order for moment integration 3335 3336 Level: advanced 3337 3338 .seealso: PetscDualSpaceLagrangeGetMomentOrder() 3339 @*/ 3340 PetscErrorCode PetscDualSpaceLagrangeSetMomentOrder(PetscDualSpace sp, PetscInt order) 3341 { 3342 PetscErrorCode ierr; 3343 3344 PetscFunctionBegin; 3345 PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1); 3346 ierr = PetscTryMethod(sp,"PetscDualSpaceLagrangeSetMomentOrder_C",(PetscDualSpace,PetscInt),(sp,order));CHKERRQ(ierr); 3347 PetscFunctionReturn(0); 3348 } 3349 3350 static PetscErrorCode PetscDualSpaceInitialize_Lagrange(PetscDualSpace sp) 3351 { 3352 PetscFunctionBegin; 3353 sp->ops->destroy = PetscDualSpaceDestroy_Lagrange; 3354 sp->ops->view = PetscDualSpaceView_Lagrange; 3355 sp->ops->setfromoptions = PetscDualSpaceSetFromOptions_Lagrange; 3356 sp->ops->duplicate = PetscDualSpaceDuplicate_Lagrange; 3357 sp->ops->setup = PetscDualSpaceSetUp_Lagrange; 3358 sp->ops->createheightsubspace = NULL; 3359 sp->ops->createpointsubspace = NULL; 3360 sp->ops->getsymmetries = PetscDualSpaceGetSymmetries_Lagrange; 3361 sp->ops->apply = PetscDualSpaceApplyDefault; 3362 sp->ops->applyall = PetscDualSpaceApplyAllDefault; 3363 sp->ops->applyint = PetscDualSpaceApplyInteriorDefault; 3364 sp->ops->createalldata = PetscDualSpaceCreateAllDataDefault; 3365 sp->ops->createintdata = PetscDualSpaceCreateInteriorDataDefault; 3366 PetscFunctionReturn(0); 3367 } 3368 3369 /*MC 3370 PETSCDUALSPACELAGRANGE = "lagrange" - A PetscDualSpace object that encapsulates a dual space of pointwise evaluation functionals 3371 3372 Level: intermediate 3373 3374 .seealso: PetscDualSpaceType, PetscDualSpaceCreate(), PetscDualSpaceSetType() 3375 M*/ 3376 PETSC_EXTERN PetscErrorCode PetscDualSpaceCreate_Lagrange(PetscDualSpace sp) 3377 { 3378 PetscDualSpace_Lag *lag; 3379 PetscErrorCode ierr; 3380 3381 PetscFunctionBegin; 3382 PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1); 3383 ierr = PetscNewLog(sp,&lag);CHKERRQ(ierr); 3384 sp->data = lag; 3385 3386 lag->tensorCell = PETSC_FALSE; 3387 lag->tensorSpace = PETSC_FALSE; 3388 lag->continuous = PETSC_TRUE; 3389 lag->numCopies = PETSC_DEFAULT; 3390 lag->numNodeSkip = PETSC_DEFAULT; 3391 lag->nodeType = PETSCDTNODES_DEFAULT; 3392 lag->useMoments = PETSC_FALSE; 3393 lag->momentOrder = 0; 3394 3395 ierr = PetscDualSpaceInitialize_Lagrange(sp);CHKERRQ(ierr); 3396 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetContinuity_C", PetscDualSpaceLagrangeGetContinuity_Lagrange);CHKERRQ(ierr); 3397 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetContinuity_C", PetscDualSpaceLagrangeSetContinuity_Lagrange);CHKERRQ(ierr); 3398 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetTensor_C", PetscDualSpaceLagrangeGetTensor_Lagrange);CHKERRQ(ierr); 3399 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetTensor_C", PetscDualSpaceLagrangeSetTensor_Lagrange);CHKERRQ(ierr); 3400 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetTrimmed_C", PetscDualSpaceLagrangeGetTrimmed_Lagrange);CHKERRQ(ierr); 3401 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetTrimmed_C", PetscDualSpaceLagrangeSetTrimmed_Lagrange);CHKERRQ(ierr); 3402 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetNodeType_C", PetscDualSpaceLagrangeGetNodeType_Lagrange);CHKERRQ(ierr); 3403 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetNodeType_C", PetscDualSpaceLagrangeSetNodeType_Lagrange);CHKERRQ(ierr); 3404 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetUseMoments_C", PetscDualSpaceLagrangeGetUseMoments_Lagrange);CHKERRQ(ierr); 3405 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetUseMoments_C", PetscDualSpaceLagrangeSetUseMoments_Lagrange);CHKERRQ(ierr); 3406 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeGetMomentOrder_C", PetscDualSpaceLagrangeGetMomentOrder_Lagrange);CHKERRQ(ierr); 3407 ierr = PetscObjectComposeFunction((PetscObject) sp, "PetscDualSpaceLagrangeSetMomentOrder_C", PetscDualSpaceLagrangeSetMomentOrder_Lagrange);CHKERRQ(ierr); 3408 PetscFunctionReturn(0); 3409 } 3410