Actual source code: febasic.c

  1: #include <petsc/private/petscfeimpl.h>
  2: #include <petscblaslapack.h>

  4: static PetscErrorCode PetscFEDestroy_Basic(PetscFE fem)
  5: {
  6:   PetscFE_Basic *b = (PetscFE_Basic *)fem->data;

  8:   PetscFunctionBegin;
  9:   PetscCall(PetscFree(b));
 10:   PetscFunctionReturn(PETSC_SUCCESS);
 11: }

 13: static PetscErrorCode PetscFEView_Basic_Ascii(PetscFE fe, PetscViewer v)
 14: {
 15:   PetscInt        dim, Nc;
 16:   PetscSpace      basis = NULL;
 17:   PetscDualSpace  dual  = NULL;
 18:   PetscQuadrature quad  = NULL;

 20:   PetscFunctionBegin;
 21:   PetscCall(PetscFEGetSpatialDimension(fe, &dim));
 22:   PetscCall(PetscFEGetNumComponents(fe, &Nc));
 23:   PetscCall(PetscFEGetBasisSpace(fe, &basis));
 24:   PetscCall(PetscFEGetDualSpace(fe, &dual));
 25:   PetscCall(PetscFEGetQuadrature(fe, &quad));
 26:   PetscCall(PetscViewerASCIIPushTab(v));
 27:   PetscCall(PetscViewerASCIIPrintf(v, "Basic Finite Element in %" PetscInt_FMT " dimensions with %" PetscInt_FMT " components\n", dim, Nc));
 28:   if (basis) PetscCall(PetscSpaceView(basis, v));
 29:   if (dual) PetscCall(PetscDualSpaceView(dual, v));
 30:   if (quad) PetscCall(PetscQuadratureView(quad, v));
 31:   PetscCall(PetscViewerASCIIPopTab(v));
 32:   PetscFunctionReturn(PETSC_SUCCESS);
 33: }

 35: static PetscErrorCode PetscFEView_Basic(PetscFE fe, PetscViewer v)
 36: {
 37:   PetscBool isascii;

 39:   PetscFunctionBegin;
 40:   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERASCII, &isascii));
 41:   if (isascii) PetscCall(PetscFEView_Basic_Ascii(fe, v));
 42:   PetscFunctionReturn(PETSC_SUCCESS);
 43: }

 45: /* Construct the change of basis from prime basis to nodal basis */
 46: PETSC_INTERN PetscErrorCode PetscFESetUp_Basic(PetscFE fem)
 47: {
 48:   PetscReal    *work;
 49:   PetscBLASInt *pivots;
 50:   PetscBLASInt  n;
 51:   PetscInt      pdim;

 53:   PetscFunctionBegin;
 54:   PetscCall(PetscDualSpaceGetDimension(fem->dualSpace, &pdim));
 55:   PetscCall(PetscMalloc1(pdim * pdim, &fem->invV));
 56:   for (PetscInt j = 0; j < pdim; ++j) {
 57:     PetscReal       *Bf;
 58:     PetscQuadrature  f;
 59:     const PetscReal *points, *weights;
 60:     PetscInt         Nc, Nq, q, k, c;

 62:     PetscCall(PetscDualSpaceGetFunctional(fem->dualSpace, j, &f));
 63:     PetscCall(PetscQuadratureGetData(f, NULL, &Nc, &Nq, &points, &weights));
 64:     PetscCall(PetscMalloc1(Nc * Nq * pdim, &Bf));
 65:     PetscCall(PetscSpaceEvaluate(fem->basisSpace, Nq, points, Bf, NULL, NULL));
 66:     for (k = 0; k < pdim; ++k) {
 67:       /* V_{jk} = n_j(\phi_k) = \int \phi_k(x) n_j(x) dx */
 68:       fem->invV[j * pdim + k] = 0.0;

 70:       for (q = 0; q < Nq; ++q) {
 71:         for (c = 0; c < Nc; ++c) fem->invV[j * pdim + k] += Bf[(q * pdim + k) * Nc + c] * weights[q * Nc + c];
 72:       }
 73:     }
 74:     PetscCall(PetscFree(Bf));
 75:   }

 77:   PetscCall(PetscMalloc2(pdim, &pivots, pdim, &work));
 78:   PetscCall(PetscBLASIntCast(pdim, &n));
 79:   PetscCallLAPACKInfo("LAPACKgetrf", LAPACKREALgetrf_(&n, &n, fem->invV, &n, pivots, &info));
 80:   PetscCallLAPACKInfo("LAPACKgetri", LAPACKREALgetri_(&n, fem->invV, &n, pivots, work, &n, &info));
 81:   PetscCall(PetscFree2(pivots, work));
 82:   PetscFunctionReturn(PETSC_SUCCESS);
 83: }

 85: PetscErrorCode PetscFEGetDimension_Basic(PetscFE fem, PetscInt *dim)
 86: {
 87:   PetscFunctionBegin;
 88:   PetscCall(PetscDualSpaceGetDimension(fem->dualSpace, dim));
 89:   PetscFunctionReturn(PETSC_SUCCESS);
 90: }

 92: /* Tensor contraction on the middle index,
 93:  *    C[m,n,p] = A[m,k,p] * B[k,n]
 94:  * where all matrices use C-style ordering.
 95:  */
 96: static PetscErrorCode TensorContract_Private(PetscInt m, PetscInt n, PetscInt p, PetscInt k, const PetscReal *A, const PetscReal *B, PetscReal *C)
 97: {
 98:   PetscFunctionBegin;
 99:   PetscCheck(n && p, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Empty tensor is not allowed %" PetscInt_FMT " %" PetscInt_FMT, n, p);
100:   for (PetscInt i = 0; i < m; i++) {
101:     PetscBLASInt n_, p_, k_, lda, ldb, ldc;
102:     PetscReal    one = 1, zero = 0;
103:     /* Taking contiguous submatrices, we wish to comput c[n,p] = a[k,p] * B[k,n]
104:      * or, in Fortran ordering, c(p,n) = a(p,k) * B(n,k)
105:      */
106:     PetscCall(PetscBLASIntCast(n, &n_));
107:     PetscCall(PetscBLASIntCast(p, &p_));
108:     PetscCall(PetscBLASIntCast(k, &k_));
109:     lda = p_;
110:     ldb = n_;
111:     ldc = p_;
112:     PetscCallBLAS("BLASgemm", BLASREALgemm_("N", "T", &p_, &n_, &k_, &one, A + i * k * p, &lda, B, &ldb, &zero, C + i * n * p, &ldc));
113:   }
114:   PetscCall(PetscLogFlops(2. * m * n * p * k));
115:   PetscFunctionReturn(PETSC_SUCCESS);
116: }

118: PETSC_INTERN PetscErrorCode PetscFEComputeTabulation_Basic(PetscFE fem, PetscInt npoints, const PetscReal points[], PetscInt K, PetscTabulation T)
119: {
120:   DM         dm;
121:   PetscInt   pdim; /* Dimension of FE space P */
122:   PetscInt   dim;  /* Spatial dimension */
123:   PetscInt   Nc;   /* Field components */
124:   PetscReal *B    = K >= 0 ? T->T[0] : NULL;
125:   PetscReal *D    = K >= 1 ? T->T[1] : NULL;
126:   PetscReal *H    = K >= 2 ? T->T[2] : NULL;
127:   PetscReal *tmpB = NULL, *tmpD = NULL, *tmpH = NULL;

129:   PetscFunctionBegin;
130:   PetscCall(PetscDualSpaceGetDM(fem->dualSpace, &dm));
131:   PetscCall(DMGetDimension(dm, &dim));
132:   PetscCall(PetscDualSpaceGetDimension(fem->dualSpace, &pdim));
133:   PetscCall(PetscFEGetNumComponents(fem, &Nc));
134:   /* Evaluate the prime basis functions at all points */
135:   if (K >= 0) PetscCall(DMGetWorkArray(dm, npoints * pdim * Nc, MPIU_REAL, &tmpB));
136:   if (K >= 1) PetscCall(DMGetWorkArray(dm, npoints * pdim * Nc * dim, MPIU_REAL, &tmpD));
137:   if (K >= 2) PetscCall(DMGetWorkArray(dm, npoints * pdim * Nc * dim * dim, MPIU_REAL, &tmpH));
138:   PetscCall(PetscSpaceEvaluate(fem->basisSpace, npoints, points, tmpB, tmpD, tmpH));
139:   /* Translate from prime to nodal basis */
140:   if (B) {
141:     /* B[npoints, nodes, Nc] = tmpB[npoints, prime, Nc] * invV[prime, nodes] */
142:     PetscCall(TensorContract_Private(npoints, pdim, Nc, pdim, tmpB, fem->invV, B));
143:   }
144:   if (D && dim) {
145:     /* D[npoints, nodes, Nc, dim] = tmpD[npoints, prime, Nc, dim] * invV[prime, nodes] */
146:     PetscCall(TensorContract_Private(npoints, pdim, Nc * dim, pdim, tmpD, fem->invV, D));
147:   }
148:   if (H && dim) {
149:     /* H[npoints, nodes, Nc, dim, dim] = tmpH[npoints, prime, Nc, dim, dim] * invV[prime, nodes] */
150:     PetscCall(TensorContract_Private(npoints, pdim, Nc * dim * dim, pdim, tmpH, fem->invV, H));
151:   }
152:   if (K >= 0) PetscCall(DMRestoreWorkArray(dm, npoints * pdim * Nc, MPIU_REAL, &tmpB));
153:   if (K >= 1) PetscCall(DMRestoreWorkArray(dm, npoints * pdim * Nc * dim, MPIU_REAL, &tmpD));
154:   if (K >= 2) PetscCall(DMRestoreWorkArray(dm, npoints * pdim * Nc * dim * dim, MPIU_REAL, &tmpH));
155:   PetscFunctionReturn(PETSC_SUCCESS);
156: }

158: PETSC_INTERN PetscErrorCode PetscFEIntegrate_Basic(PetscDS ds, PetscInt field, PetscInt Ne, PetscFEGeom *cgeom, const PetscScalar coefficients[], PetscDS dsAux, const PetscScalar coefficientsAux[], PetscScalar integral[])
159: {
160:   const PetscInt     debug = ds->printIntegrate;
161:   PetscFE            fe;
162:   PetscPointFn      *obj_func;
163:   PetscQuadrature    quad;
164:   PetscTabulation   *T, *TAux = NULL;
165:   PetscScalar       *u, *u_x, *a, *a_x;
166:   const PetscScalar *constants;
167:   PetscReal         *x, cellScale;
168:   PetscInt          *uOff, *uOff_x, *aOff = NULL, *aOff_x = NULL;
169:   PetscInt           dim, dE, Np, numConstants, Nf, NfAux = 0, totDim, totDimAux = 0, cOffset = 0, cOffsetAux = 0, e;
170:   PetscBool          isAffine;
171:   const PetscReal   *quadPoints, *quadWeights;
172:   PetscInt           qNc, Nq, q;

174:   PetscFunctionBegin;
175:   PetscCall(PetscDSGetObjective(ds, field, &obj_func));
176:   if (!obj_func) PetscFunctionReturn(PETSC_SUCCESS);
177:   PetscCall(PetscDSGetDiscretization(ds, field, (PetscObject *)&fe));
178:   PetscCall(PetscFEGetSpatialDimension(fe, &dim));
179:   cellScale = (PetscReal)PetscPowInt(2, dim);
180:   PetscCall(PetscFEGetQuadrature(fe, &quad));
181:   PetscCall(PetscDSGetNumFields(ds, &Nf));
182:   PetscCall(PetscDSGetTotalDimension(ds, &totDim));
183:   PetscCall(PetscDSGetComponentOffsets(ds, &uOff));
184:   PetscCall(PetscDSGetComponentDerivativeOffsets(ds, &uOff_x));
185:   PetscCall(PetscDSGetTabulation(ds, &T));
186:   PetscCall(PetscDSGetEvaluationArrays(ds, &u, NULL, &u_x));
187:   PetscCall(PetscDSGetWorkspace(ds, &x, NULL, NULL, NULL, NULL));
188:   PetscCall(PetscDSSetIntegrationParameters(ds, field, PETSC_DETERMINE));
189:   PetscCall(PetscDSGetConstants(ds, &numConstants, &constants));
190:   if (dsAux) {
191:     PetscCall(PetscDSGetNumFields(dsAux, &NfAux));
192:     PetscCall(PetscDSGetTotalDimension(dsAux, &totDimAux));
193:     PetscCall(PetscDSGetComponentOffsets(dsAux, &aOff));
194:     PetscCall(PetscDSGetComponentDerivativeOffsets(dsAux, &aOff_x));
195:     PetscCall(PetscDSGetTabulation(dsAux, &TAux));
196:     PetscCall(PetscDSGetEvaluationArrays(dsAux, &a, NULL, &a_x));
197:     PetscCheck(T[0]->Np == TAux[0]->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of tabulation points %" PetscInt_FMT " != %" PetscInt_FMT " number of auxiliary tabulation points", T[0]->Np, TAux[0]->Np);
198:   }
199:   PetscCall(PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights));
200:   PetscCheck(qNc == 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only supports scalar quadrature, not %" PetscInt_FMT " components", qNc);
201:   Np       = cgeom->numPoints;
202:   dE       = cgeom->dimEmbed;
203:   isAffine = cgeom->isAffine;
204:   for (e = 0; e < Ne; ++e) {
205:     PetscFEGeom fegeom;

207:     fegeom.dim      = cgeom->dim;
208:     fegeom.dimEmbed = cgeom->dimEmbed;
209:     fegeom.xi       = NULL;
210:     if (isAffine) {
211:       fegeom.v    = x;
212:       fegeom.xi   = cgeom->xi;
213:       fegeom.J    = &cgeom->J[e * Np * dE * dE];
214:       fegeom.invJ = &cgeom->invJ[e * Np * dE * dE];
215:       fegeom.detJ = &cgeom->detJ[e * Np];
216:     } else fegeom.xi = NULL;
217:     for (q = 0; q < Nq; ++q) {
218:       PetscScalar integrand = 0.;
219:       PetscReal   w;

221:       if (isAffine) {
222:         CoordinatesRefToReal(dE, dim, fegeom.xi, &cgeom->v[e * Np * dE], fegeom.J, &quadPoints[q * dim], x);
223:       } else {
224:         fegeom.v    = &cgeom->v[(e * Np + q) * dE];
225:         fegeom.J    = &cgeom->J[(e * Np + q) * dE * dE];
226:         fegeom.invJ = &cgeom->invJ[(e * Np + q) * dE * dE];
227:         fegeom.detJ = &cgeom->detJ[e * Np + q];
228:       }
229:       PetscCall(PetscDSSetCellParameters(ds, fegeom.detJ[0] * cellScale));
230:       w = fegeom.detJ[0] * quadWeights[q];
231:       if (debug > 1 && q < Np) {
232:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "  detJ: %g\n", (double)fegeom.detJ[0]));
233: #if !PetscDefined(USE_COMPLEX)
234:         PetscCall(DMPrintCellMatrix(e, "invJ", dim, dim, fegeom.invJ));
235: #endif
236:       }
237:       if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  quad point %" PetscInt_FMT "\n", q));
238:       PetscCall(PetscFEEvaluateFieldJets_Internal(ds, Nf, 0, q, T, &fegeom, &coefficients[cOffset], NULL, u, u_x, NULL));
239:       if (dsAux) PetscCall(PetscFEEvaluateFieldJets_Internal(dsAux, NfAux, 0, q, TAux, &fegeom, &coefficientsAux[cOffsetAux], NULL, a, a_x, NULL));
240:       obj_func(dim, Nf, NfAux, uOff, uOff_x, u, NULL, u_x, aOff, aOff_x, a, NULL, a_x, 0.0, fegeom.v, numConstants, constants, &integrand);
241:       integrand *= w;
242:       integral[e * Nf + field] += integrand;
243:     }
244:     if (debug > 1) PetscCall(PetscPrintf(PETSC_COMM_SELF, "    Element Field %" PetscInt_FMT " integral: %g\n", Nf, (double)PetscRealPart(integral[e * Nf + field])));
245:     cOffset += totDim;
246:     cOffsetAux += totDimAux;
247:   }
248:   PetscFunctionReturn(PETSC_SUCCESS);
249: }

251: PETSC_INTERN PetscErrorCode PetscFEIntegrateBd_Basic(PetscDS ds, PetscInt field, PetscBdPointFn *obj_func, PetscInt Ne, PetscFEGeom *fgeom, const PetscScalar coefficients[], PetscDS dsAux, const PetscScalar coefficientsAux[], PetscScalar integral[])
252: {
253:   const PetscInt     debug = ds->printIntegrate;
254:   PetscFE            fe;
255:   PetscQuadrature    quad;
256:   PetscTabulation   *Tf, *TfAux = NULL;
257:   PetscScalar       *u, *u_x, *a, *a_x, *basisReal, *basisDerReal;
258:   const PetscScalar *constants;
259:   PetscReal         *x, cellScale;
260:   PetscInt          *uOff, *uOff_x, *aOff = NULL, *aOff_x = NULL;
261:   PetscBool          isAffine, auxOnBd;
262:   const PetscReal   *quadPoints, *quadWeights;
263:   PetscInt           qNc, Nq, q, Np, dE;
264:   PetscInt           dim, dimAux, numConstants, Nf, NfAux = 0, totDim, totDimAux = 0, cOffset = 0, cOffsetAux = 0, e;

266:   PetscFunctionBegin;
267:   if (!obj_func) PetscFunctionReturn(PETSC_SUCCESS);
268:   PetscCall(PetscDSGetDiscretization(ds, field, (PetscObject *)&fe));
269:   PetscCall(PetscFEGetSpatialDimension(fe, &dim));
270:   cellScale = (PetscReal)PetscPowInt(2, dim);
271:   PetscCall(PetscFEGetFaceQuadrature(fe, &quad));
272:   PetscCall(PetscDSGetNumFields(ds, &Nf));
273:   PetscCall(PetscDSGetTotalDimension(ds, &totDim));
274:   PetscCall(PetscDSGetComponentOffsets(ds, &uOff));
275:   PetscCall(PetscDSGetComponentDerivativeOffsets(ds, &uOff_x));
276:   PetscCall(PetscDSGetEvaluationArrays(ds, &u, NULL, &u_x));
277:   PetscCall(PetscDSGetWorkspace(ds, &x, &basisReal, &basisDerReal, NULL, NULL));
278:   PetscCall(PetscDSGetFaceTabulation(ds, &Tf));
279:   PetscCall(PetscDSSetIntegrationParameters(ds, field, PETSC_DETERMINE));
280:   PetscCall(PetscDSGetConstants(ds, &numConstants, &constants));
281:   if (dsAux) {
282:     PetscCall(PetscDSGetSpatialDimension(dsAux, &dimAux));
283:     PetscCall(PetscDSGetNumFields(dsAux, &NfAux));
284:     PetscCall(PetscDSGetTotalDimension(dsAux, &totDimAux));
285:     PetscCall(PetscDSGetComponentOffsets(dsAux, &aOff));
286:     PetscCall(PetscDSGetComponentDerivativeOffsets(dsAux, &aOff_x));
287:     PetscCall(PetscDSGetEvaluationArrays(dsAux, &a, NULL, &a_x));
288:     auxOnBd = dimAux < dim ? PETSC_TRUE : PETSC_FALSE;
289:     if (auxOnBd) PetscCall(PetscDSGetTabulation(dsAux, &TfAux));
290:     else PetscCall(PetscDSGetFaceTabulation(dsAux, &TfAux));
291:     PetscCheck(Tf[0]->Np == TfAux[0]->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of tabulation points %" PetscInt_FMT " != %" PetscInt_FMT " number of auxiliary tabulation points", Tf[0]->Np, TfAux[0]->Np);
292:   }
293:   PetscCall(PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights));
294:   PetscCheck(qNc == 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only supports scalar quadrature, not %" PetscInt_FMT " components", qNc);
295:   if (debug > 1) PetscCall(PetscPrintf(PETSC_COMM_SELF, "Field: %" PetscInt_FMT " Nface: %" PetscInt_FMT " Nq: %" PetscInt_FMT "\n", field, Ne, Nq));
296:   Np       = fgeom->numPoints;
297:   dE       = fgeom->dimEmbed;
298:   isAffine = fgeom->isAffine;
299:   for (e = 0; e < Ne; ++e) {
300:     PetscFEGeom    fegeom, cgeom;
301:     const PetscInt face = fgeom->face[e][0]; /* Local face number in cell */
302:     fegeom.n            = NULL;
303:     fegeom.v            = NULL;
304:     fegeom.xi           = NULL;
305:     fegeom.J            = NULL;
306:     fegeom.invJ         = NULL;
307:     fegeom.detJ         = NULL;
308:     fegeom.dim          = fgeom->dim;
309:     fegeom.dimEmbed     = fgeom->dimEmbed;
310:     cgeom.dim           = fgeom->dim;
311:     cgeom.dimEmbed      = fgeom->dimEmbed;
312:     if (isAffine) {
313:       fegeom.v    = x;
314:       fegeom.xi   = fgeom->xi;
315:       fegeom.J    = &fgeom->J[e * Np * dE * dE];
316:       fegeom.invJ = &fgeom->invJ[e * Np * dE * dE];
317:       fegeom.detJ = &fgeom->detJ[e * Np];
318:       fegeom.n    = &fgeom->n[e * Np * dE];

320:       cgeom.J    = &fgeom->suppJ[0][e * Np * dE * dE];
321:       cgeom.invJ = &fgeom->suppInvJ[0][e * Np * dE * dE];
322:       cgeom.detJ = &fgeom->suppDetJ[0][e * Np];
323:     } else fegeom.xi = NULL;
324:     for (q = 0; q < Nq; ++q) {
325:       PetscScalar integrand = 0.;
326:       PetscReal   w;

328:       if (isAffine) {
329:         CoordinatesRefToReal(dE, dim - 1, fegeom.xi, &fgeom->v[e * Np * dE], fegeom.J, &quadPoints[q * (dim - 1)], x);
330:       } else {
331:         fegeom.v    = &fgeom->v[(e * Np + q) * dE];
332:         fegeom.J    = &fgeom->J[(e * Np + q) * dE * dE];
333:         fegeom.invJ = &fgeom->invJ[(e * Np + q) * dE * dE];
334:         fegeom.detJ = &fgeom->detJ[e * Np + q];
335:         fegeom.n    = &fgeom->n[(e * Np + q) * dE];

337:         cgeom.J    = &fgeom->suppJ[0][(e * Np + q) * dE * dE];
338:         cgeom.invJ = &fgeom->suppInvJ[0][(e * Np + q) * dE * dE];
339:         cgeom.detJ = &fgeom->suppDetJ[0][e * Np + q];
340:       }
341:       PetscCall(PetscDSSetCellParameters(ds, fegeom.detJ[0] * cellScale));
342:       w = fegeom.detJ[0] * quadWeights[q];
343:       if (debug > 1 && q < Np) {
344:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "  detJ: %g\n", (double)fegeom.detJ[0]));
345: #if !PetscDefined(USE_COMPLEX)
346:         PetscCall(DMPrintCellMatrix(e, "invJ", dim, dim, fegeom.invJ));
347: #endif
348:       }
349:       if (debug > 1) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  quad point %" PetscInt_FMT "\n", q));
350:       if (debug > 3) {
351:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "    x_q ("));
352:         for (PetscInt d = 0; d < dE; ++d) {
353:           if (d) PetscCall(PetscPrintf(PETSC_COMM_SELF, ", "));
354:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "%g", (double)fegeom.v[d]));
355:         }
356:         PetscCall(PetscPrintf(PETSC_COMM_SELF, ")\n"));
357:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "    n_q ("));
358:         for (PetscInt d = 0; d < dE; ++d) {
359:           if (d) PetscCall(PetscPrintf(PETSC_COMM_SELF, ", "));
360:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "%g", (double)fegeom.n[d]));
361:         }
362:         PetscCall(PetscPrintf(PETSC_COMM_SELF, ")\n"));
363:         for (PetscInt f = 0; f < Nf; ++f) {
364:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "    u_%" PetscInt_FMT " (", f));
365:           for (PetscInt c = 0; c < uOff[f + 1] - uOff[f]; ++c) {
366:             if (c) PetscCall(PetscPrintf(PETSC_COMM_SELF, ", "));
367:             PetscCall(PetscPrintf(PETSC_COMM_SELF, "%g", (double)PetscRealPart(u[uOff[f] + c])));
368:           }
369:           PetscCall(PetscPrintf(PETSC_COMM_SELF, ")\n"));
370:         }
371:       }
372:       PetscCall(PetscFEEvaluateFieldJets_Internal(ds, Nf, face, q, Tf, &cgeom, &coefficients[cOffset], NULL, u, u_x, NULL));
373:       if (dsAux) PetscCall(PetscFEEvaluateFieldJets_Internal(dsAux, NfAux, face, q, TfAux, &cgeom, &coefficientsAux[cOffsetAux], NULL, a, a_x, NULL));
374:       obj_func(dim, Nf, NfAux, uOff, uOff_x, u, NULL, u_x, aOff, aOff_x, a, NULL, a_x, 0.0, fegeom.v, fegeom.n, numConstants, constants, &integrand);
375:       integrand *= w;
376:       integral[e * Nf + field] += integrand;
377:       if (debug > 1) PetscCall(PetscPrintf(PETSC_COMM_SELF, "    int: %g tot: %g\n", (double)PetscRealPart(integrand), (double)PetscRealPart(integral[e * Nf + field])));
378:     }
379:     cOffset += totDim;
380:     cOffsetAux += totDimAux;
381:   }
382:   PetscFunctionReturn(PETSC_SUCCESS);
383: }

385: PetscErrorCode PetscFEIntegrateResidual_Basic(PetscDS ds, PetscFormKey key, PetscInt Ne, PetscFEGeom *cgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS dsAux, const PetscScalar coefficientsAux[], PetscReal t, PetscScalar elemVec[])
386: {
387:   const PetscInt     debug = ds->printIntegrate;
388:   const PetscInt     field = key.field;
389:   PetscFE            fe;
390:   PetscWeakForm      wf;
391:   PetscInt           n0, n1, i;
392:   PetscPointFn     **f0_func, **f1_func;
393:   PetscQuadrature    quad;
394:   PetscTabulation   *T, *TAux = NULL;
395:   PetscScalar       *f0, *f1, *u, *u_t = NULL, *u_x, *a, *a_x, *basisReal, *basisDerReal;
396:   const PetscScalar *constants;
397:   PetscReal         *x, cellScale;
398:   PetscInt          *uOff, *uOff_x, *aOff = NULL, *aOff_x = NULL;
399:   PetscInt           dim, numConstants, Nf, NfAux = 0, totDim, totDimAux = 0, cOffset = 0, cOffsetAux = 0, fOffset, e;
400:   const PetscReal   *quadPoints, *quadWeights;
401:   PetscInt           qdim, qNc, Nq, q, dE;

403:   PetscFunctionBegin;
404:   PetscCall(PetscDSGetDiscretization(ds, field, (PetscObject *)&fe));
405:   PetscCall(PetscFEGetSpatialDimension(fe, &dim));
406:   cellScale = (PetscReal)PetscPowInt(2, dim);
407:   PetscCall(PetscFEGetQuadrature(fe, &quad));
408:   PetscCall(PetscDSGetNumFields(ds, &Nf));
409:   PetscCall(PetscDSGetTotalDimension(ds, &totDim));
410:   PetscCall(PetscDSGetComponentOffsets(ds, &uOff));
411:   PetscCall(PetscDSGetComponentDerivativeOffsets(ds, &uOff_x));
412:   PetscCall(PetscDSGetFieldOffset(ds, field, &fOffset));
413:   PetscCall(PetscDSGetWeakForm(ds, &wf));
414:   PetscCall(PetscWeakFormGetResidual(wf, key.label, key.value, key.field, key.part, &n0, &f0_func, &n1, &f1_func));
415:   if (!n0 && !n1) PetscFunctionReturn(PETSC_SUCCESS);
416:   PetscCall(PetscDSGetEvaluationArrays(ds, &u, coefficients_t ? &u_t : NULL, &u_x));
417:   PetscCall(PetscDSGetWorkspace(ds, &x, &basisReal, &basisDerReal, NULL, NULL));
418:   PetscCall(PetscDSGetWeakFormArrays(ds, &f0, &f1, NULL, NULL, NULL, NULL));
419:   PetscCall(PetscDSGetTabulation(ds, &T));
420:   PetscCall(PetscDSSetIntegrationParameters(ds, field, PETSC_DETERMINE));
421:   PetscCall(PetscDSGetConstants(ds, &numConstants, &constants));
422:   if (dsAux) {
423:     PetscCall(PetscDSGetNumFields(dsAux, &NfAux));
424:     PetscCall(PetscDSGetTotalDimension(dsAux, &totDimAux));
425:     PetscCall(PetscDSGetComponentOffsets(dsAux, &aOff));
426:     PetscCall(PetscDSGetComponentDerivativeOffsets(dsAux, &aOff_x));
427:     PetscCall(PetscDSGetEvaluationArrays(dsAux, &a, NULL, &a_x));
428:     PetscCall(PetscDSGetTabulation(dsAux, &TAux));
429:     PetscCheck(T[0]->Np == TAux[0]->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of tabulation points %" PetscInt_FMT " != %" PetscInt_FMT " number of auxiliary tabulation points", T[0]->Np, TAux[0]->Np);
430:   }
431:   PetscCall(PetscQuadratureGetData(quad, &qdim, &qNc, &Nq, &quadPoints, &quadWeights));
432:   PetscCheck(qNc == 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only supports scalar quadrature, not %" PetscInt_FMT " components", qNc);
433:   dE = cgeom->dimEmbed;
434:   PetscCheck(cgeom->dim == qdim, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "FEGeom dim %" PetscInt_FMT " != %" PetscInt_FMT " quadrature dim", cgeom->dim, qdim);
435:   for (e = 0; e < Ne; ++e) {
436:     PetscFEGeom fegeom;

438:     fegeom.v = x; /* workspace */
439:     PetscCall(PetscArrayzero(f0, Nq * T[field]->Nc));
440:     PetscCall(PetscArrayzero(f1, Nq * T[field]->Nc * dE));
441:     for (q = 0; q < Nq; ++q) {
442:       PetscReal w;
443:       PetscInt  c, d;

445:       PetscCall(PetscFEGeomGetPoint(cgeom, e, q, &quadPoints[q * cgeom->dim], &fegeom));
446:       PetscCall(PetscDSSetCellParameters(ds, fegeom.detJ[0] * cellScale));
447:       w = fegeom.detJ[0] * quadWeights[q];
448:       if (debug > 1 && q < cgeom->numPoints) {
449:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "  detJ: %g\n", (double)fegeom.detJ[0]));
450: #if !PetscDefined(USE_COMPLEX)
451:         PetscCall(DMPrintCellMatrix(e, "invJ", dE, dE, fegeom.invJ));
452: #endif
453:       }
454:       PetscCall(PetscFEEvaluateFieldJets_Internal(ds, Nf, 0, q, T, &fegeom, &coefficients[cOffset], PetscSafePointerPlusOffset(coefficients_t, cOffset), u, u_x, u_t));
455:       if (dsAux) PetscCall(PetscFEEvaluateFieldJets_Internal(dsAux, NfAux, 0, q, TAux, &fegeom, &coefficientsAux[cOffsetAux], NULL, a, a_x, NULL));
456:       for (i = 0; i < n0; ++i) f0_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, fegeom.v, numConstants, constants, &f0[q * T[field]->Nc]);
457:       for (c = 0; c < T[field]->Nc; ++c) f0[q * T[field]->Nc + c] *= w;
458:       for (i = 0; i < n1; ++i) f1_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, fegeom.v, numConstants, constants, &f1[q * T[field]->Nc * dE]);
459:       for (c = 0; c < T[field]->Nc; ++c)
460:         for (d = 0; d < dE; ++d) f1[(q * T[field]->Nc + c) * dE + d] *= w;
461:       if (debug) {
462:         // LCOV_EXCL_START
463:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "  quad point %" PetscInt_FMT " wt %g x:", q, (double)quadWeights[q]));
464:         for (c = 0; c < dE; ++c) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %g", (double)fegeom.v[c]));
465:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
466:         if (debug > 2) {
467:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "  field %" PetscInt_FMT ":", field));
468:           for (c = 0; c < T[field]->Nc; ++c) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %g", (double)PetscRealPart(u[uOff[field] + c])));
469:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
470:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "  field der %" PetscInt_FMT ":", field));
471:           for (c = 0; c < T[field]->Nc * dE; ++c) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %g", (double)PetscRealPart(u_x[uOff[field] + c])));
472:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
473:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "  resid %" PetscInt_FMT ":", field));
474:           for (c = 0; c < T[field]->Nc; ++c) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %g", (double)PetscRealPart(f0[q * T[field]->Nc + c])));
475:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
476:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "  res der %" PetscInt_FMT ":", field));
477:           for (c = 0; c < T[field]->Nc; ++c) {
478:             for (d = 0; d < dE; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %g", (double)PetscRealPart(f1[(q * T[field]->Nc + c) * dE + d])));
479:           }
480:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
481:         }
482:         // LCOV_EXCL_STOP
483:       }
484:     }
485:     PetscCall(PetscFEUpdateElementVec_Internal(fe, T[field], 0, NULL, basisReal, basisDerReal, e, cgeom, f0, f1, &elemVec[cOffset + fOffset]));
486:     cOffset += totDim;
487:     cOffsetAux += totDimAux;
488:   }
489:   PetscFunctionReturn(PETSC_SUCCESS);
490: }

492: /*
493:   Return the map from physical face quadrature points to face tabulation points for a face with orientation ornt in its cell.
494:   The tabulation point of physical point q is the preimage of q under ornt, given by the permutation of the inverse orientation.
495: */
496: static PetscErrorCode PetscFEGetFaceQuadraturePermutation_Private(PetscQuadrature fq, PetscInt ornt, PetscInt *Na, IS *perms[], IS *permIS, const PetscInt *qperm[])
497: {
498:   DMPolytopeType ct;

500:   PetscFunctionBegin;
501:   *permIS = NULL;
502:   *qperm  = NULL;
503:   if (!ornt) PetscFunctionReturn(PETSC_SUCCESS);
504:   PetscCall(PetscQuadratureGetPermutations(fq, Na, perms));
505:   PetscCall(PetscQuadratureGetCellType(fq, &ct));
506:   *permIS = (*perms)[DMPolytopeTypeComposeOrientationInv(ct, 0, ornt) + *Na / 2];
507:   PetscCall(ISGetIndices(*permIS, qperm));
508:   PetscFunctionReturn(PETSC_SUCCESS);
509: }

511: static PetscErrorCode PetscFERestoreFaceQuadraturePermutation_Private(IS permIS, const PetscInt *qperm[])
512: {
513:   PetscFunctionBegin;
514:   if (permIS != NULL) PetscCall(ISRestoreIndices(permIS, qperm));
515:   PetscFunctionReturn(PETSC_SUCCESS);
516: }

518: PetscErrorCode PetscFEIntegrateBdResidual_Basic(PetscDS ds, PetscWeakForm wf, PetscFormKey key, PetscInt Ne, PetscFEGeom *fgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS dsAux, const PetscScalar coefficientsAux[], PetscReal t, PetscScalar elemVec[])
519: {
520:   const PetscInt     debug = ds->printIntegrate;
521:   const PetscInt     field = key.field;
522:   PetscFE            fe;
523:   PetscInt           n0, n1, i;
524:   PetscBdPointFn   **f0_func, **f1_func;
525:   PetscQuadrature    quad;
526:   PetscTabulation   *Tf, *TfAux = NULL;
527:   PetscScalar       *f0, *f1, *u, *u_t = NULL, *u_x, *a, *a_x, *basisReal, *basisDerReal;
528:   const PetscScalar *constants;
529:   PetscReal         *x, cellScale;
530:   PetscInt          *uOff, *uOff_x, *aOff = NULL, *aOff_x = NULL;
531:   PetscInt           dim, dimAux, numConstants, Nf, NfAux = 0, totDim, totDimAux = 0, cOffset = 0, cOffsetAux = 0, fOffset, e, NcI;
532:   PetscBool          auxOnBd = PETSC_FALSE;
533:   const PetscReal   *quadPoints, *quadWeights;
534:   PetscInt           qdim, qNc, Nq, q, dE, Na = 0;
535:   IS                *perms = NULL;

537:   PetscFunctionBegin;
538:   PetscCall(PetscDSGetDiscretization(ds, field, (PetscObject *)&fe));
539:   PetscCall(PetscFEGetSpatialDimension(fe, &dim));
540:   cellScale = (PetscReal)PetscPowInt(2, dim);
541:   PetscCall(PetscFEGetFaceQuadrature(fe, &quad));
542:   PetscCall(PetscDSGetNumFields(ds, &Nf));
543:   PetscCall(PetscDSGetTotalDimension(ds, &totDim));
544:   PetscCall(PetscDSGetComponentOffsets(ds, &uOff));
545:   PetscCall(PetscDSGetComponentDerivativeOffsets(ds, &uOff_x));
546:   PetscCall(PetscDSGetFieldOffset(ds, field, &fOffset));
547:   PetscCall(PetscWeakFormGetBdResidual(wf, key.label, key.value, key.field, key.part, &n0, &f0_func, &n1, &f1_func));
548:   if (!n0 && !n1) PetscFunctionReturn(PETSC_SUCCESS);
549:   PetscCall(PetscDSGetEvaluationArrays(ds, &u, coefficients_t ? &u_t : NULL, &u_x));
550:   PetscCall(PetscDSGetWorkspace(ds, &x, &basisReal, &basisDerReal, NULL, NULL));
551:   PetscCall(PetscDSGetWeakFormArrays(ds, &f0, &f1, NULL, NULL, NULL, NULL));
552:   PetscCall(PetscDSGetFaceTabulation(ds, &Tf));
553:   PetscCall(PetscDSSetIntegrationParameters(ds, field, PETSC_DETERMINE));
554:   PetscCall(PetscDSGetConstants(ds, &numConstants, &constants));
555:   if (dsAux) {
556:     PetscCall(PetscDSGetSpatialDimension(dsAux, &dimAux));
557:     PetscCall(PetscDSGetNumFields(dsAux, &NfAux));
558:     PetscCall(PetscDSGetTotalDimension(dsAux, &totDimAux));
559:     PetscCall(PetscDSGetComponentOffsets(dsAux, &aOff));
560:     PetscCall(PetscDSGetComponentDerivativeOffsets(dsAux, &aOff_x));
561:     PetscCall(PetscDSGetEvaluationArrays(dsAux, &a, NULL, &a_x));
562:     auxOnBd = dimAux < dim ? PETSC_TRUE : PETSC_FALSE;
563:     if (auxOnBd) PetscCall(PetscDSGetTabulation(dsAux, &TfAux));
564:     else PetscCall(PetscDSGetFaceTabulation(dsAux, &TfAux));
565:     PetscCheck(Tf[0]->Np == TfAux[0]->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of tabulation points %" PetscInt_FMT " != %" PetscInt_FMT " number of auxiliary tabulation points", Tf[0]->Np, TfAux[0]->Np);
566:   }
567:   NcI = Tf[field]->Nc;
568:   PetscCall(PetscQuadratureGetData(quad, &qdim, &qNc, &Nq, &quadPoints, &quadWeights));
569:   PetscCheck(qNc == 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only supports scalar quadrature, not %" PetscInt_FMT " components", qNc);
570:   dE = fgeom->dimEmbed;
571:   /* TODO FIX THIS */
572:   fgeom->dim = dim - 1;
573:   PetscCheck(fgeom->dim == qdim, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "FEGeom dim %" PetscInt_FMT " != %" PetscInt_FMT " quadrature dim", fgeom->dim, qdim);
574:   for (e = 0; e < Ne; ++e) {
575:     PetscFEGeom     fegeom, cgeom;
576:     const PetscInt  face = fgeom->face[e][0];
577:     const PetscInt  ornt = fgeom->face[e][1];
578:     const PetscInt *qperm;
579:     IS              permIS;

581:     fegeom.v = x; /* Workspace */
582:     PetscCall(PetscArrayzero(f0, Nq * NcI));
583:     PetscCall(PetscArrayzero(f1, Nq * NcI * dE));
584:     PetscCall(PetscFEGetFaceQuadraturePermutation_Private(quad, ornt, &Na, &perms, &permIS, &qperm));
585:     for (q = 0; q < Nq; ++q) {
586:       PetscReal      w;
587:       PetscInt       c;
588:       const PetscInt qp = qperm != NULL ? qperm[q] : q;

590:       PetscCall(PetscFEGeomGetPoint(fgeom, e, q, &quadPoints[q * fgeom->dim], &fegeom));
591:       PetscCall(PetscFEGeomGetCellPoint(fgeom, e, q, &cgeom));
592:       PetscCall(PetscDSSetCellParameters(ds, fegeom.detJ[0] * cellScale));
593:       w = fegeom.detJ[0] * quadWeights[q];
594:       if (debug > 1) {
595:         if ((fgeom->isAffine && q == 0) || !fgeom->isAffine) {
596:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "  detJ: %g\n", (double)fegeom.detJ[0]));
597: #if !PetscDefined(USE_COMPLEX)
598:           PetscCall(DMPrintCellMatrix(e, "invJ", dim, dim, fegeom.invJ));
599:           PetscCall(DMPrintCellVector(e, "n", dim, fegeom.n));
600: #endif
601:         }
602:       }
603:       PetscCall(PetscFEEvaluateFieldJets_Internal(ds, Nf, face, qp, Tf, &cgeom, &coefficients[cOffset], PetscSafePointerPlusOffset(coefficients_t, cOffset), u, u_x, u_t));
604:       if (dsAux) PetscCall(PetscFEEvaluateFieldJets_Internal(dsAux, NfAux, auxOnBd ? 0 : face, qp, TfAux, &cgeom, &coefficientsAux[cOffsetAux], NULL, a, a_x, NULL));
605:       for (i = 0; i < n0; ++i) f0_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, fegeom.v, fegeom.n, numConstants, constants, &f0[q * NcI]);
606:       for (c = 0; c < NcI; ++c) f0[q * NcI + c] *= w;
607:       for (i = 0; i < n1; ++i) f1_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, fegeom.v, fegeom.n, numConstants, constants, &f1[q * NcI * dE]);
608:       for (c = 0; c < NcI; ++c)
609:         for (PetscInt d = 0; d < dE; ++d) f1[(q * NcI + c) * dE + d] *= w;
610:       if (debug) {
611:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "  elem %" PetscInt_FMT " quad point %" PetscInt_FMT "\n", e, q));
612:         for (c = 0; c < NcI; ++c) {
613:           if (n0) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  f0[%" PetscInt_FMT "] %g\n", c, (double)PetscRealPart(f0[q * NcI + c])));
614:           if (n1) {
615:             for (PetscInt d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  f1[%" PetscInt_FMT ",%" PetscInt_FMT "] %g", c, d, (double)PetscRealPart(f1[(q * NcI + c) * dim + d])));
616:             PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
617:           }
618:         }
619:       }
620:     }
621:     PetscCall(PetscFEUpdateElementVec_Internal(fe, Tf[field], face, qperm, basisReal, basisDerReal, e, fgeom, f0, f1, &elemVec[cOffset + fOffset]));
622:     PetscCall(PetscFERestoreFaceQuadraturePermutation_Private(permIS, &qperm));
623:     cOffset += totDim;
624:     cOffsetAux += totDimAux;
625:   }
626:   PetscFunctionReturn(PETSC_SUCCESS);
627: }

629: /*
630:   BdIntegral: Operates completely in the embedding dimension. The trick is to have special "face quadrature" so we only integrate over the face, but
631:               all transforms operate in the full space and are square.

633:   HybridIntegral: The discretization is lower dimensional. That means the transforms are non-square.
634:     1) DMPlexGetCellFields() retrieves from the hybrid cell, so it gets fields from both faces
635:     2) We need to assume that the orientation is 0 for both
636:     3) TODO We need to use a non-square Jacobian for the derivative maps, meaning the embedding dimension has to go to EvaluateFieldJets() and UpdateElementVec()
637: */
638: PETSC_INTERN PetscErrorCode PetscFEIntegrateHybridResidual_Basic(PetscDS ds, PetscDS dsIn, PetscFormKey key, PetscInt s, PetscInt Ne, PetscFEGeom *fgeom, PetscFEGeom *nbrgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS dsAux, const PetscScalar coefficientsAux[], PetscReal t, PetscScalar elemVec[])
639: {
640:   const PetscInt     debug = ds->printIntegrate;
641:   const PetscInt     field = key.field;
642:   PetscFE            fe;
643:   PetscWeakForm      wf;
644:   PetscInt           n0, n1, i;
645:   PetscBdPointFn   **f0_func, **f1_func;
646:   PetscQuadrature    quad;
647:   DMPolytopeType     ct;
648:   PetscTabulation   *Tf, *TfIn, *TfAux = NULL;
649:   PetscScalar       *f0, *f1, *u, *u_t = NULL, *u_x, *a, *a_x, *basisReal, *basisDerReal;
650:   const PetscScalar *constants;
651:   PetscReal         *x;
652:   PetscInt          *uOff, *uOff_x, *aOff = NULL, *aOff_x = NULL;
653:   PetscInt           dim, dimAux, numConstants, Nf, NfAux = 0, totDim, totDimIn, totDimAux = 0, cOffset = 0, cOffsetIn = 0, cOffsetAux = 0, fOffset, e, NcI, NcS;
654:   PetscBool          isCohesiveField, auxOnBd = PETSC_FALSE;
655:   const PetscReal   *quadPoints, *quadWeights;
656:   PetscInt           qdim, qNc, Nq, q, dE;

658:   PetscFunctionBegin;
659:   /* Hybrid discretization is posed directly on faces */
660:   PetscCall(PetscDSGetDiscretization(ds, field, (PetscObject *)&fe));
661:   PetscCall(PetscFEGetSpatialDimension(fe, &dim));
662:   PetscCall(PetscFEGetQuadrature(fe, &quad));
663:   PetscCall(PetscDSGetNumFields(ds, &Nf));
664:   PetscCall(PetscDSGetTotalDimension(ds, &totDim));
665:   PetscCall(PetscDSGetTotalDimension(dsIn, &totDimIn));
666:   PetscCall(PetscDSGetComponentOffsetsCohesive(dsIn, 0, &uOff)); // Change 0 to s for one-sided offsets
667:   PetscCall(PetscDSGetComponentDerivativeOffsetsCohesive(dsIn, s, &uOff_x));
668:   PetscCall(PetscDSGetFieldOffsetCohesive(ds, field, &fOffset));
669:   PetscCall(PetscDSGetWeakForm(ds, &wf));
670:   PetscCall(PetscWeakFormGetBdResidual(wf, key.label, key.value, key.field, key.part, &n0, &f0_func, &n1, &f1_func));
671:   if (!n0 && !n1) PetscFunctionReturn(PETSC_SUCCESS);
672:   PetscCall(PetscDSGetEvaluationArrays(ds, &u, coefficients_t ? &u_t : NULL, &u_x));
673:   PetscCall(PetscDSGetWorkspace(ds, &x, &basisReal, &basisDerReal, NULL, NULL));
674:   PetscCall(PetscDSGetWeakFormArrays(ds, &f0, &f1, NULL, NULL, NULL, NULL));
675:   /* NOTE This is a bulk tabulation because the DS is a face discretization */
676:   PetscCall(PetscDSGetTabulation(ds, &Tf));
677:   PetscCall(PetscDSGetFaceTabulation(dsIn, &TfIn));
678:   PetscCall(PetscDSSetIntegrationParameters(ds, field, PETSC_DETERMINE));
679:   PetscCall(PetscDSGetConstants(ds, &numConstants, &constants));
680:   if (dsAux) {
681:     PetscCall(PetscDSGetSpatialDimension(dsAux, &dimAux));
682:     PetscCall(PetscDSGetNumFields(dsAux, &NfAux));
683:     PetscCall(PetscDSGetTotalDimension(dsAux, &totDimAux));
684:     PetscCall(PetscDSGetComponentOffsets(dsAux, &aOff));
685:     PetscCall(PetscDSGetComponentDerivativeOffsets(dsAux, &aOff_x));
686:     PetscCall(PetscDSGetEvaluationArrays(dsAux, &a, NULL, &a_x));
687:     auxOnBd = dimAux == dim ? PETSC_TRUE : PETSC_FALSE;
688:     if (auxOnBd) PetscCall(PetscDSGetTabulation(dsAux, &TfAux));
689:     else PetscCall(PetscDSGetFaceTabulation(dsAux, &TfAux));
690:     PetscCheck(Tf[0]->Np == TfAux[0]->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of tabulation points %" PetscInt_FMT " != %" PetscInt_FMT " number of auxiliary tabulation points", Tf[0]->Np, TfAux[0]->Np);
691:   }
692:   PetscCall(PetscDSGetCohesive(ds, field, &isCohesiveField));
693:   NcI = Tf[field]->Nc;
694:   NcS = NcI;
695:   if (!isCohesiveField && s == 2) {
696:     // If we are integrating over a cohesive cell (s = 2) for a non-cohesive fields, we use both sides
697:     NcS *= 2;
698:   }
699:   PetscCall(PetscQuadratureGetData(quad, &qdim, &qNc, &Nq, &quadPoints, &quadWeights));
700:   PetscCall(PetscQuadratureGetCellType(quad, &ct));
701:   PetscCheck(qNc == 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only supports scalar quadrature, not %" PetscInt_FMT " components", qNc);
702:   dE = fgeom->dimEmbed;
703:   PetscCheck(fgeom->dim == qdim, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "FEGeom dim %" PetscInt_FMT " != %" PetscInt_FMT " quadrature dim", fgeom->dim, qdim);
704:   for (e = 0; e < Ne; ++e) {
705:     // In order for the face information to be correct, the support of endcap faces _must_ be correctly oriented
706:     PetscFEGeom    fegeom, fegeomN[2];
707:     const PetscInt face[2]  = {fgeom->face[e * 2 + 0][0], fgeom->face[e * 2 + 1][2]};
708:     const PetscInt ornt[2]  = {fgeom->face[e * 2 + 0][1], fgeom->face[e * 2 + 1][3]};
709:     const PetscInt cornt[2] = {fgeom->face[e * 2 + 0][3], fgeom->face[e * 2 + 1][1]};

711:     fegeom.v = x; /* Workspace */
712:     PetscCall(PetscArrayzero(f0, Nq * NcS));
713:     PetscCall(PetscArrayzero(f1, Nq * NcS * dE));
714:     if (debug > 2) {
715:       PetscCall(PetscPrintf(PETSC_COMM_SELF, "Negative %s face: %" PetscInt_FMT " (%" PetscInt_FMT ") (%" PetscInt_FMT ") perm %" PetscInt_FMT "\n", DMPolytopeTypes[ct], face[0], ornt[0], cornt[0], DMPolytopeTypeComposeOrientationInv(ct, cornt[0], ornt[0])));
716:       PetscCall(PetscPrintf(PETSC_COMM_SELF, "Positive %s face: %" PetscInt_FMT " (%" PetscInt_FMT ") (%" PetscInt_FMT ") perm %" PetscInt_FMT "\n", DMPolytopeTypes[ct], face[1], ornt[1], cornt[1], DMPolytopeTypeComposeOrientationInv(ct, cornt[1], ornt[1])));
717:     }
718:     for (q = 0; q < Nq; ++q) {
719:       PetscInt  qpt[2];
720:       PetscReal w;
721:       PetscInt  c;

723:       PetscCall(PetscDSPermuteQuadPoint(ds, DMPolytopeTypeComposeOrientationInv(ct, cornt[0], ornt[0]), field, q, &qpt[0]));
724:       PetscCall(PetscDSPermuteQuadPoint(ds, DMPolytopeTypeComposeOrientationInv(ct, cornt[1], ornt[1]), field, q, &qpt[1]));
725:       PetscCall(PetscFEGeomGetPoint(fgeom, e * 2, q, &quadPoints[q * fgeom->dim], &fegeom));
726:       PetscCall(PetscFEGeomGetPoint(nbrgeom, e * 2, q, NULL, &fegeomN[0]));
727:       PetscCall(PetscFEGeomGetPoint(nbrgeom, e * 2 + 1, q, NULL, &fegeomN[1]));
728:       w = fegeom.detJ[0] * quadWeights[q];
729:       if (debug > 1 && q < fgeom->numPoints) {
730:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "  detJ: %g\n", (double)fegeom.detJ[0]));
731: #if !PetscDefined(USE_COMPLEX)
732:         PetscCall(DMPrintCellMatrix(e, "invJ", dim, dE, fegeom.invJ));
733: #endif
734:       }
735:       if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  quad point %" PetscInt_FMT " weight %g detJ %g\n", q, (double)quadWeights[q], (double)fegeom.detJ[0]));
736:       /* TODO Is this cell or face quadrature, meaning should we use 'q' or 'face*Nq+q' */
737:       PetscCall(PetscFEEvaluateFieldJets_Hybrid_Internal(dsIn, Nf, 0, q, Tf, face, qpt, TfIn, &fegeom, fegeomN, &coefficients[cOffsetIn], PetscSafePointerPlusOffset(coefficients_t, cOffsetIn), u, u_x, u_t));
738:       if (dsAux) PetscCall(PetscFEEvaluateFieldJets_Internal(dsAux, NfAux, auxOnBd ? 0 : face[s], auxOnBd ? q : qpt[s], TfAux, &fegeom, &coefficientsAux[cOffsetAux], NULL, a, a_x, NULL));
739:       for (i = 0; i < n0; ++i) f0_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, fegeom.v, fegeom.n, numConstants, constants, &f0[q * NcS]);
740:       for (c = 0; c < NcS; ++c) f0[q * NcS + c] *= w;
741:       for (i = 0; i < n1; ++i) f1_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, fegeom.v, fegeom.n, numConstants, constants, &f1[q * NcS * dE]);
742:       for (c = 0; c < NcS; ++c)
743:         for (PetscInt d = 0; d < dE; ++d) f1[(q * NcS + c) * dE + d] *= w;
744:       if (debug) {
745:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "  elem %" PetscInt_FMT " quad point %" PetscInt_FMT " field %" PetscInt_FMT " side %" PetscInt_FMT "\n", e, q, field, s));
746:         for (PetscInt f = 0; f < Nf; ++f) {
747:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Field %" PetscInt_FMT ":", f));
748:           for (PetscInt c = uOff[f]; c < uOff[f + 1]; ++c) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  %g", (double)PetscRealPart(u[c])));
749:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
750:         }
751:         for (c = 0; c < NcS; ++c) {
752:           if (n0) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  f0[%" PetscInt_FMT "] %g\n", c, (double)PetscRealPart(f0[q * NcS + c])));
753:           if (n1) {
754:             for (PetscInt d = 0; d < dE; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  f1[%" PetscInt_FMT ",%" PetscInt_FMT "] %g", c, d, (double)PetscRealPart(f1[(q * NcS + c) * dE + d])));
755:             PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
756:           }
757:         }
758:       }
759:     }
760:     if (isCohesiveField) {
761:       PetscCall(PetscFEUpdateElementVec_Internal(fe, Tf[field], 0, NULL, basisReal, basisDerReal, e, fgeom, f0, f1, &elemVec[cOffset + fOffset]));
762:     } else {
763:       PetscCall(PetscFEUpdateElementVec_Hybrid_Internal(fe, Tf[field], 0, s, basisReal, basisDerReal, fgeom, f0, f1, &elemVec[cOffset + fOffset]));
764:     }
765:     cOffset += totDim;
766:     cOffsetIn += totDimIn;
767:     cOffsetAux += totDimAux;
768:   }
769:   PetscFunctionReturn(PETSC_SUCCESS);
770: }

772: PetscErrorCode PetscFEIntegrateJacobian_Basic(PetscDS rds, PetscDS cds, PetscFEJacobianType jtype, PetscFormKey key, PetscInt Ne, PetscFEGeom *cgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS dsAux, const PetscScalar coefficientsAux[], PetscReal t, PetscReal u_tshift, PetscScalar elemMat[])
773: {
774:   const PetscInt     debug = rds->printIntegrate;
775:   PetscFE            feI, feJ;
776:   PetscWeakForm      wf;
777:   PetscPointJacFn  **g0_func, **g1_func, **g2_func, **g3_func;
778:   PetscInt           n0, n1, n2, n3;
779:   PetscInt           cOffset    = 0; /* Offset into coefficients[] for element e */
780:   PetscInt           cOffsetAux = 0; /* Offset into coefficientsAux[] for element e */
781:   PetscInt           eOffset    = 0; /* Offset into elemMat[] for element e */
782:   PetscInt           offsetI    = 0; /* Offset into an element vector for fieldI */
783:   PetscInt           offsetJ    = 0; /* Offset into an element vector for fieldJ */
784:   PetscQuadrature    quad;
785:   PetscTabulation   *rT, *cT, *TAux = NULL;
786:   PetscScalar       *g0 = NULL, *g1 = NULL, *g2 = NULL, *g3 = NULL, *u, *u_t = NULL, *u_x, *a, *a_x, *basisReal, *basisDerReal, *testReal, *testDerReal;
787:   const PetscScalar *constants;
788:   PetscReal         *x, cellScale;
789:   PetscInt          *uOff, *uOff_x, *aOff = NULL, *aOff_x = NULL;
790:   PetscInt           NcI = 0, NcJ = 0;
791:   PetscInt           dim, numConstants, Nf, fieldI, fieldJ, NfAux = 0, rtotDim, ctotDim, totDimAux = 0;
792:   PetscInt           dE, Np;
793:   PetscBool          isAffine;
794:   const PetscReal   *quadPoints, *quadWeights;
795:   PetscInt           qNc, Nq;

797:   PetscFunctionBegin;
798:   PetscCall(PetscDSGetNumFields(rds, &Nf));
799:   fieldI = key.field / Nf;
800:   fieldJ = key.field % Nf;
801:   PetscCall(PetscDSGetDiscretization(rds, fieldI, (PetscObject *)&feI));
802:   PetscCall(PetscDSGetDiscretization(cds, fieldJ, (PetscObject *)&feJ));
803:   PetscCall(PetscFEGetSpatialDimension(feI, &dim));
804:   cellScale = (PetscReal)PetscPowInt(2, dim);
805:   PetscCall(PetscFEGetQuadrature(feI, &quad));
806:   PetscCall(PetscDSGetTotalDimension(rds, &rtotDim));
807:   PetscCall(PetscDSGetTotalDimension(cds, &ctotDim));
808:   PetscCall(PetscDSGetComponentOffsets(rds, &uOff));
809:   PetscCall(PetscDSGetComponentDerivativeOffsets(rds, &uOff_x));
810:   PetscCall(PetscDSGetWeakForm(rds, &wf));
811:   switch (jtype) {
812:   case PETSCFE_JACOBIAN_DYN:
813:     PetscCall(PetscWeakFormGetDynamicJacobian(wf, key.label, key.value, fieldI, fieldJ, key.part, &n0, &g0_func, &n1, &g1_func, &n2, &g2_func, &n3, &g3_func));
814:     break;
815:   case PETSCFE_JACOBIAN_PRE:
816:     PetscCall(PetscWeakFormGetJacobianPreconditioner(wf, key.label, key.value, fieldI, fieldJ, key.part, &n0, &g0_func, &n1, &g1_func, &n2, &g2_func, &n3, &g3_func));
817:     break;
818:   case PETSCFE_JACOBIAN:
819:     PetscCall(PetscWeakFormGetJacobian(wf, key.label, key.value, fieldI, fieldJ, key.part, &n0, &g0_func, &n1, &g1_func, &n2, &g2_func, &n3, &g3_func));
820:     break;
821:   }
822:   if (!n0 && !n1 && !n2 && !n3) PetscFunctionReturn(PETSC_SUCCESS);
823:   PetscCall(PetscDSGetEvaluationArrays(rds, &u, coefficients_t ? &u_t : NULL, &u_x));
824:   PetscCall(PetscDSGetWorkspace(rds, &x, &basisReal, &basisDerReal, &testReal, &testDerReal));
825:   PetscCall(PetscDSGetWeakFormArrays(rds, NULL, NULL, n0 ? &g0 : NULL, n1 ? &g1 : NULL, n2 ? &g2 : NULL, n3 ? &g3 : NULL));

827:   PetscCall(PetscDSGetTabulation(rds, &rT));
828:   PetscCall(PetscDSGetTabulation(cds, &cT));
829:   PetscCheck(rT[0]->Np == cT[0]->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of row tabulation points %" PetscInt_FMT " != %" PetscInt_FMT " number of col tabulation points", rT[0]->Np, cT[0]->Np);
830:   PetscCall(PetscDSGetFieldOffset(rds, fieldI, &offsetI));
831:   PetscCall(PetscDSGetFieldOffset(cds, fieldJ, &offsetJ));
832:   PetscCall(PetscDSSetIntegrationParameters(rds, fieldI, fieldJ));
833:   PetscCall(PetscDSSetIntegrationParameters(cds, fieldI, fieldJ));
834:   PetscCall(PetscDSGetConstants(rds, &numConstants, &constants));
835:   if (dsAux) {
836:     PetscCall(PetscDSGetNumFields(dsAux, &NfAux));
837:     PetscCall(PetscDSGetTotalDimension(dsAux, &totDimAux));
838:     PetscCall(PetscDSGetComponentOffsets(dsAux, &aOff));
839:     PetscCall(PetscDSGetComponentDerivativeOffsets(dsAux, &aOff_x));
840:     PetscCall(PetscDSGetEvaluationArrays(dsAux, &a, NULL, &a_x));
841:     PetscCall(PetscDSGetTabulation(dsAux, &TAux));
842:     PetscCheck(rT[0]->Np == TAux[0]->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of tabulation points %" PetscInt_FMT " != %" PetscInt_FMT " number of auxiliary tabulation points", rT[0]->Np, TAux[0]->Np);
843:   }
844:   NcI      = rT[fieldI]->Nc;
845:   NcJ      = cT[fieldJ]->Nc;
846:   Np       = cgeom->numPoints;
847:   dE       = cgeom->dimEmbed;
848:   isAffine = cgeom->isAffine;
849:   PetscCall(PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights));
850:   PetscCheck(qNc == 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only supports scalar quadrature, not %" PetscInt_FMT " components", qNc);

852:   for (PetscInt e = 0; e < Ne; ++e) {
853:     PetscFEGeom fegeom;

855:     fegeom.dim      = cgeom->dim;
856:     fegeom.dimEmbed = cgeom->dimEmbed;
857:     fegeom.xi       = NULL;
858:     if (isAffine) {
859:       fegeom.v    = x;
860:       fegeom.xi   = cgeom->xi;
861:       fegeom.J    = &cgeom->J[e * Np * dE * dE];
862:       fegeom.invJ = &cgeom->invJ[e * Np * dE * dE];
863:       fegeom.detJ = &cgeom->detJ[e * Np];
864:     } else fegeom.xi = NULL;
865:     for (PetscInt q = 0; q < Nq; ++q) {
866:       PetscReal w;

868:       if (isAffine) {
869:         CoordinatesRefToReal(dE, dim, fegeom.xi, &cgeom->v[e * Np * dE], fegeom.J, &quadPoints[q * dim], x);
870:       } else {
871:         fegeom.v    = &cgeom->v[(e * Np + q) * dE];
872:         fegeom.J    = &cgeom->J[(e * Np + q) * dE * dE];
873:         fegeom.invJ = &cgeom->invJ[(e * Np + q) * dE * dE];
874:         fegeom.detJ = &cgeom->detJ[e * Np + q];
875:       }
876:       PetscCall(PetscDSSetCellParameters(rds, fegeom.detJ[0] * cellScale));
877:       if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  quad point %" PetscInt_FMT " weight %g detJ %g\n", q, (double)quadWeights[q], (double)fegeom.detJ[0]));
878:       w = fegeom.detJ[0] * quadWeights[q];
879:       if (coefficients) PetscCall(PetscFEEvaluateFieldJets_Internal(rds, Nf, 0, q, rT, &fegeom, &coefficients[cOffset], PetscSafePointerPlusOffset(coefficients_t, cOffset), u, u_x, u_t));
880:       if (dsAux) PetscCall(PetscFEEvaluateFieldJets_Internal(dsAux, NfAux, 0, q, TAux, &fegeom, &coefficientsAux[cOffsetAux], NULL, a, a_x, NULL));
881:       if (n0) {
882:         PetscCall(PetscArrayzero(g0, NcI * NcJ));
883:         for (PetscInt i = 0; i < n0; ++i) g0_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, u_tshift, fegeom.v, numConstants, constants, g0);
884:         for (PetscInt c = 0; c < NcI * NcJ; ++c) g0[c] *= w;
885:       }
886:       if (n1) {
887:         PetscCall(PetscArrayzero(g1, NcI * NcJ * dE));
888:         for (PetscInt i = 0; i < n1; ++i) g1_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, u_tshift, fegeom.v, numConstants, constants, g1);
889:         for (PetscInt c = 0; c < NcI * NcJ * dE; ++c) g1[c] *= w;
890:       }
891:       if (n2) {
892:         PetscCall(PetscArrayzero(g2, NcI * NcJ * dE));
893:         for (PetscInt i = 0; i < n2; ++i) g2_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, u_tshift, fegeom.v, numConstants, constants, g2);
894:         for (PetscInt c = 0; c < NcI * NcJ * dE; ++c) g2[c] *= w;
895:       }
896:       if (n3) {
897:         PetscCall(PetscArrayzero(g3, NcI * NcJ * dE * dE));
898:         for (PetscInt i = 0; i < n3; ++i) g3_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, u_tshift, fegeom.v, numConstants, constants, g3);
899:         for (PetscInt c = 0; c < NcI * NcJ * dE * dE; ++c) g3[c] *= w;
900:       }

902:       PetscCall(PetscFEUpdateElementMat_Internal(feI, feJ, 0, q, rT[fieldI], basisReal, basisDerReal, cT[fieldJ], testReal, testDerReal, &fegeom, g0, g1, g2, g3, ctotDim, offsetI, offsetJ, elemMat + eOffset));
903:     }
904:     if (debug > 1) {
905:       PetscCall(PetscPrintf(PETSC_COMM_SELF, "Element matrix for fields %" PetscInt_FMT " and %" PetscInt_FMT "\n", fieldI, fieldJ));
906:       for (PetscInt f = 0; f < rT[fieldI]->Nb; ++f) {
907:         const PetscInt i = offsetI + f;
908:         for (PetscInt g = 0; g < cT[fieldJ]->Nb; ++g) {
909:           const PetscInt j = offsetJ + g;
910:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "    elemMat[%" PetscInt_FMT ", %" PetscInt_FMT "]: %g\n", f, g, (double)PetscRealPart(elemMat[eOffset + i * ctotDim + j])));
911:         }
912:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
913:       }
914:     }
915:     cOffset += rtotDim;
916:     cOffsetAux += totDimAux;
917:     eOffset += rtotDim * ctotDim;
918:   }
919:   PetscFunctionReturn(PETSC_SUCCESS);
920: }

922: PETSC_INTERN PetscErrorCode PetscFEIntegrateBdJacobian_Basic(PetscDS ds, PetscWeakForm wf, PetscFEJacobianType jtype, PetscFormKey key, PetscInt Ne, PetscFEGeom *fgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS dsAux, const PetscScalar coefficientsAux[], PetscReal t, PetscReal u_tshift, PetscScalar elemMat[])
923: {
924:   const PetscInt      debug = ds->printIntegrate;
925:   PetscFE             feI, feJ;
926:   PetscBdPointJacFn **g0_func, **g1_func, **g2_func, **g3_func;
927:   PetscInt            n0, n1, n2, n3, i;
928:   PetscInt            cOffset    = 0; /* Offset into coefficients[] for element e */
929:   PetscInt            cOffsetAux = 0; /* Offset into coefficientsAux[] for element e */
930:   PetscInt            eOffset    = 0; /* Offset into elemMat[] for element e */
931:   PetscInt            offsetI    = 0; /* Offset into an element vector for fieldI */
932:   PetscInt            offsetJ    = 0; /* Offset into an element vector for fieldJ */
933:   PetscQuadrature     quad;
934:   PetscTabulation    *T, *TAux = NULL;
935:   PetscScalar        *g0, *g1, *g2, *g3, *u, *u_t = NULL, *u_x, *a, *a_x, *basisReal, *basisDerReal, *testReal, *testDerReal;
936:   const PetscScalar  *constants;
937:   PetscReal          *x, cellScale;
938:   PetscInt           *uOff, *uOff_x, *aOff = NULL, *aOff_x = NULL;
939:   PetscInt            NcI = 0, NcJ = 0;
940:   PetscInt            dim, numConstants, Nf, fieldI, fieldJ, NfAux = 0, totDim, totDimAux = 0, e;
941:   PetscBool           isAffine;
942:   const PetscReal    *quadPoints, *quadWeights;
943:   PetscInt            qNc, Nq, q, Np, dE, Na = 0;
944:   IS                 *perms = NULL;

946:   PetscFunctionBegin;
947:   PetscCall(PetscDSGetNumFields(ds, &Nf));
948:   fieldI = key.field / Nf;
949:   fieldJ = key.field % Nf;
950:   PetscCall(PetscDSGetDiscretization(ds, fieldI, (PetscObject *)&feI));
951:   PetscCall(PetscDSGetDiscretization(ds, fieldJ, (PetscObject *)&feJ));
952:   PetscCall(PetscFEGetSpatialDimension(feI, &dim));
953:   cellScale = (PetscReal)PetscPowInt(2, dim);
954:   PetscCall(PetscFEGetFaceQuadrature(feI, &quad));
955:   PetscCall(PetscDSGetTotalDimension(ds, &totDim));
956:   PetscCall(PetscDSGetComponentOffsets(ds, &uOff));
957:   PetscCall(PetscDSGetComponentDerivativeOffsets(ds, &uOff_x));
958:   PetscCall(PetscDSGetFieldOffset(ds, fieldI, &offsetI));
959:   PetscCall(PetscDSGetFieldOffset(ds, fieldJ, &offsetJ));
960:   switch (jtype) {
961:   case PETSCFE_JACOBIAN_PRE:
962:     PetscCall(PetscWeakFormGetBdJacobianPreconditioner(wf, key.label, key.value, fieldI, fieldJ, key.part, &n0, &g0_func, &n1, &g1_func, &n2, &g2_func, &n3, &g3_func));
963:     break;
964:   case PETSCFE_JACOBIAN:
965:     PetscCall(PetscWeakFormGetBdJacobian(wf, key.label, key.value, fieldI, fieldJ, key.part, &n0, &g0_func, &n1, &g1_func, &n2, &g2_func, &n3, &g3_func));
966:     break;
967:   case PETSCFE_JACOBIAN_DYN:
968:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "PETSCFE_JACOBIAN_DYN is not supported for PetscFEIntegrateBdJacobian()");
969:   }
970:   if (!n0 && !n1 && !n2 && !n3) PetscFunctionReturn(PETSC_SUCCESS);
971:   PetscCall(PetscDSGetEvaluationArrays(ds, &u, coefficients_t ? &u_t : NULL, &u_x));
972:   PetscCall(PetscDSGetWorkspace(ds, &x, &basisReal, &basisDerReal, &testReal, &testDerReal));
973:   PetscCall(PetscDSGetWeakFormArrays(ds, NULL, NULL, &g0, &g1, &g2, &g3));
974:   PetscCall(PetscDSGetFaceTabulation(ds, &T));
975:   PetscCall(PetscDSSetIntegrationParameters(ds, fieldI, fieldJ));
976:   PetscCall(PetscDSGetConstants(ds, &numConstants, &constants));
977:   if (dsAux) {
978:     PetscCall(PetscDSGetNumFields(dsAux, &NfAux));
979:     PetscCall(PetscDSGetTotalDimension(dsAux, &totDimAux));
980:     PetscCall(PetscDSGetComponentOffsets(dsAux, &aOff));
981:     PetscCall(PetscDSGetComponentDerivativeOffsets(dsAux, &aOff_x));
982:     PetscCall(PetscDSGetEvaluationArrays(dsAux, &a, NULL, &a_x));
983:     PetscCall(PetscDSGetFaceTabulation(dsAux, &TAux));
984:   }
985:   NcI = T[fieldI]->Nc, NcJ = T[fieldJ]->Nc;
986:   Np       = fgeom->numPoints;
987:   dE       = fgeom->dimEmbed;
988:   isAffine = fgeom->isAffine;
989:   PetscCheck(NcI * NcJ * dE >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid Jacobian block size %" PetscInt_FMT, NcI * NcJ * dE);
990:   /* Initialize here in case the function is not defined */
991:   PetscCall(PetscArrayzero(g0, NcI * NcJ));
992:   PetscCall(PetscArrayzero(g1, NcI * NcJ * dE));
993:   PetscCall(PetscArrayzero(g2, NcI * NcJ * dE));
994:   PetscCall(PetscArrayzero(g3, NcI * NcJ * dE * dE));
995:   PetscCall(PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights));
996:   PetscCheck(qNc == 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only supports scalar quadrature, not %" PetscInt_FMT " components", qNc);
997:   for (e = 0; e < Ne; ++e) {
998:     PetscFEGeom     fegeom, cgeom;
999:     const PetscInt  face = fgeom->face[e][0];
1000:     const PetscInt  ornt = fgeom->face[e][1];
1001:     const PetscInt *qperm;
1002:     IS              permIS;

1004:     fegeom.n        = NULL;
1005:     fegeom.v        = NULL;
1006:     fegeom.xi       = NULL;
1007:     fegeom.J        = NULL;
1008:     fegeom.detJ     = NULL;
1009:     fegeom.dim      = fgeom->dim;
1010:     fegeom.dimEmbed = fgeom->dimEmbed;
1011:     cgeom.dim       = fgeom->dim;
1012:     cgeom.dimEmbed  = fgeom->dimEmbed;
1013:     if (isAffine) {
1014:       fegeom.v    = x;
1015:       fegeom.xi   = fgeom->xi;
1016:       fegeom.J    = &fgeom->J[e * Np * dE * dE];
1017:       fegeom.invJ = &fgeom->invJ[e * Np * dE * dE];
1018:       fegeom.detJ = &fgeom->detJ[e * Np];
1019:       fegeom.n    = &fgeom->n[e * Np * dE];

1021:       cgeom.J    = &fgeom->suppJ[0][e * Np * dE * dE];
1022:       cgeom.invJ = &fgeom->suppInvJ[0][e * Np * dE * dE];
1023:       cgeom.detJ = &fgeom->suppDetJ[0][e * Np];
1024:     } else fegeom.xi = NULL;
1025:     PetscCall(PetscFEGetFaceQuadraturePermutation_Private(quad, ornt, &Na, &perms, &permIS, &qperm));
1026:     for (q = 0; q < Nq; ++q) {
1027:       PetscReal      w;
1028:       const PetscInt qp = qperm != NULL ? qperm[q] : q;

1030:       if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  quad point %" PetscInt_FMT "\n", q));
1031:       if (isAffine) {
1032:         CoordinatesRefToReal(dE, dim - 1, fegeom.xi, &fgeom->v[e * Np * dE], fegeom.J, &quadPoints[q * (dim - 1)], x);
1033:       } else {
1034:         fegeom.v    = &fgeom->v[(e * Np + q) * dE];
1035:         fegeom.J    = &fgeom->J[(e * Np + q) * dE * dE];
1036:         fegeom.invJ = &fgeom->invJ[(e * Np + q) * dE * dE];
1037:         fegeom.detJ = &fgeom->detJ[e * Np + q];
1038:         fegeom.n    = &fgeom->n[(e * Np + q) * dE];

1040:         cgeom.J    = &fgeom->suppJ[0][(e * Np + q) * dE * dE];
1041:         cgeom.invJ = &fgeom->suppInvJ[0][(e * Np + q) * dE * dE];
1042:         cgeom.detJ = &fgeom->suppDetJ[0][e * Np + q];
1043:       }
1044:       PetscCall(PetscDSSetCellParameters(ds, fegeom.detJ[0] * cellScale));
1045:       w = fegeom.detJ[0] * quadWeights[q];
1046:       if (coefficients) PetscCall(PetscFEEvaluateFieldJets_Internal(ds, Nf, face, qp, T, &cgeom, &coefficients[cOffset], PetscSafePointerPlusOffset(coefficients_t, cOffset), u, u_x, u_t));
1047:       if (dsAux) PetscCall(PetscFEEvaluateFieldJets_Internal(dsAux, NfAux, face, qp, TAux, &cgeom, &coefficientsAux[cOffsetAux], NULL, a, a_x, NULL));
1048:       if (n0) {
1049:         PetscCall(PetscArrayzero(g0, NcI * NcJ));
1050:         for (i = 0; i < n0; ++i) g0_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, u_tshift, fegeom.v, fegeom.n, numConstants, constants, g0);
1051:         for (PetscInt c = 0; c < NcI * NcJ; ++c) g0[c] *= w;
1052:       }
1053:       if (n1) {
1054:         PetscCall(PetscArrayzero(g1, NcI * NcJ * dE));
1055:         for (i = 0; i < n1; ++i) g1_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, u_tshift, fegeom.v, fegeom.n, numConstants, constants, g1);
1056:         for (PetscInt c = 0; c < NcI * NcJ * dE; ++c) g1[c] *= w;
1057:       }
1058:       if (n2) {
1059:         PetscCall(PetscArrayzero(g2, NcI * NcJ * dE));
1060:         for (i = 0; i < n2; ++i) g2_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, u_tshift, fegeom.v, fegeom.n, numConstants, constants, g2);
1061:         for (PetscInt c = 0; c < NcI * NcJ * dE; ++c) g2[c] *= w;
1062:       }
1063:       if (n3) {
1064:         PetscCall(PetscArrayzero(g3, NcI * NcJ * dE * dE));
1065:         for (i = 0; i < n3; ++i) g3_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, u_tshift, fegeom.v, fegeom.n, numConstants, constants, g3);
1066:         for (PetscInt c = 0; c < NcI * NcJ * dE * dE; ++c) g3[c] *= w;
1067:       }

1069:       PetscCall(PetscFEUpdateElementMat_Internal(feI, feJ, face, qp, T[fieldI], basisReal, basisDerReal, T[fieldJ], testReal, testDerReal, &cgeom, g0, g1, g2, g3, totDim, offsetI, offsetJ, elemMat + eOffset));
1070:     }
1071:     PetscCall(PetscFERestoreFaceQuadraturePermutation_Private(permIS, &qperm));
1072:     if (debug > 1) {
1073:       PetscInt fc, f, gc, g;

1075:       PetscCall(PetscPrintf(PETSC_COMM_SELF, "Element matrix for fields %" PetscInt_FMT " and %" PetscInt_FMT "\n", fieldI, fieldJ));
1076:       for (fc = 0; fc < T[fieldI]->Nc; ++fc) {
1077:         for (f = 0; f < T[fieldI]->Nb; ++f) {
1078:           const PetscInt i = offsetI + f * T[fieldI]->Nc + fc;
1079:           for (gc = 0; gc < T[fieldJ]->Nc; ++gc) {
1080:             for (g = 0; g < T[fieldJ]->Nb; ++g) {
1081:               const PetscInt j = offsetJ + g * T[fieldJ]->Nc + gc;
1082:               PetscCall(PetscPrintf(PETSC_COMM_SELF, "    elemMat[%" PetscInt_FMT ",%" PetscInt_FMT ",%" PetscInt_FMT ",%" PetscInt_FMT "]: %g\n", f, fc, g, gc, (double)PetscRealPart(elemMat[eOffset + i * totDim + j])));
1083:             }
1084:           }
1085:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1086:         }
1087:       }
1088:     }
1089:     cOffset += totDim;
1090:     cOffsetAux += totDimAux;
1091:     eOffset += PetscSqr(totDim);
1092:   }
1093:   PetscFunctionReturn(PETSC_SUCCESS);
1094: }

1096: PETSC_INTERN PetscErrorCode PetscFEIntegrateHybridJacobian_Basic(PetscDS ds, PetscDS dsIn, PetscFEJacobianType jtype, PetscFormKey key, PetscInt s, PetscInt Ne, PetscFEGeom *fgeom, PetscFEGeom *nbrgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS dsAux, const PetscScalar coefficientsAux[], PetscReal t, PetscReal u_tshift, PetscScalar elemMat[])
1097: {
1098:   const PetscInt      debug = ds->printIntegrate;
1099:   PetscFE             feI, feJ;
1100:   PetscWeakForm       wf;
1101:   PetscBdPointJacFn **g0_func, **g1_func, **g2_func, **g3_func;
1102:   PetscInt            n0, n1, n2, n3, i;
1103:   PetscInt            cOffset    = 0; /* Offset into coefficients[] for element e */
1104:   PetscInt            cOffsetAux = 0; /* Offset into coefficientsAux[] for element e */
1105:   PetscInt            eOffset    = 0; /* Offset into elemMat[] for element e */
1106:   PetscInt            offsetI    = 0; /* Offset into an element vector for fieldI */
1107:   PetscInt            offsetJ    = 0; /* Offset into an element vector for fieldJ */
1108:   PetscQuadrature     quad;
1109:   DMPolytopeType      ct;
1110:   PetscTabulation    *T, *TfIn, *TAux = NULL;
1111:   PetscScalar        *g0, *g1, *g2, *g3, *u, *u_t = NULL, *u_x, *a, *a_x, *basisReal, *basisDerReal, *testReal, *testDerReal;
1112:   const PetscScalar  *constants;
1113:   PetscReal          *x;
1114:   PetscInt           *uOff, *uOff_x, *aOff = NULL, *aOff_x = NULL;
1115:   PetscInt            NcI = 0, NcJ = 0, NcS, NcT;
1116:   PetscInt            dim, dimAux, numConstants, Nf, fieldI, fieldJ, NfAux = 0, totDim, totDimAux = 0, e;
1117:   PetscBool           isCohesiveFieldI, isCohesiveFieldJ, auxOnBd = PETSC_FALSE;
1118:   const PetscReal    *quadPoints, *quadWeights;
1119:   PetscInt            qNc, Nq, q, dE;

1121:   PetscFunctionBegin;
1122:   PetscCall(PetscDSGetNumFields(ds, &Nf));
1123:   fieldI = key.field / Nf;
1124:   fieldJ = key.field % Nf;
1125:   /* Hybrid discretization is posed directly on faces */
1126:   PetscCall(PetscDSGetDiscretization(ds, fieldI, (PetscObject *)&feI));
1127:   PetscCall(PetscDSGetDiscretization(ds, fieldJ, (PetscObject *)&feJ));
1128:   PetscCall(PetscFEGetSpatialDimension(feI, &dim));
1129:   PetscCall(PetscFEGetQuadrature(feI, &quad));
1130:   PetscCall(PetscDSGetTotalDimension(ds, &totDim));
1131:   PetscCall(PetscDSGetComponentOffsetsCohesive(ds, 0, &uOff)); // Change 0 to s for one-sided offsets
1132:   PetscCall(PetscDSGetComponentDerivativeOffsetsCohesive(ds, s, &uOff_x));
1133:   PetscCall(PetscDSGetWeakForm(ds, &wf));
1134:   switch (jtype) {
1135:   case PETSCFE_JACOBIAN_PRE:
1136:     PetscCall(PetscWeakFormGetBdJacobianPreconditioner(wf, key.label, key.value, fieldI, fieldJ, key.part, &n0, &g0_func, &n1, &g1_func, &n2, &g2_func, &n3, &g3_func));
1137:     break;
1138:   case PETSCFE_JACOBIAN:
1139:     PetscCall(PetscWeakFormGetBdJacobian(wf, key.label, key.value, fieldI, fieldJ, key.part, &n0, &g0_func, &n1, &g1_func, &n2, &g2_func, &n3, &g3_func));
1140:     break;
1141:   case PETSCFE_JACOBIAN_DYN:
1142:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No boundary hybrid Jacobians :)");
1143:   }
1144:   if (!n0 && !n1 && !n2 && !n3) PetscFunctionReturn(PETSC_SUCCESS);
1145:   PetscCall(PetscDSGetEvaluationArrays(ds, &u, coefficients_t ? &u_t : NULL, &u_x));
1146:   PetscCall(PetscDSGetWorkspace(ds, &x, &basisReal, &basisDerReal, &testReal, &testDerReal));
1147:   PetscCall(PetscDSGetWeakFormArrays(ds, NULL, NULL, &g0, &g1, &g2, &g3));
1148:   PetscCall(PetscDSGetTabulation(ds, &T));
1149:   PetscCall(PetscDSGetFaceTabulation(dsIn, &TfIn));
1150:   PetscCall(PetscDSGetFieldOffsetCohesive(ds, fieldI, &offsetI));
1151:   PetscCall(PetscDSGetFieldOffsetCohesive(ds, fieldJ, &offsetJ));
1152:   PetscCall(PetscDSSetIntegrationParameters(ds, fieldI, fieldJ));
1153:   PetscCall(PetscDSGetConstants(ds, &numConstants, &constants));
1154:   if (dsAux) {
1155:     PetscCall(PetscDSGetSpatialDimension(dsAux, &dimAux));
1156:     PetscCall(PetscDSGetNumFields(dsAux, &NfAux));
1157:     PetscCall(PetscDSGetTotalDimension(dsAux, &totDimAux));
1158:     PetscCall(PetscDSGetComponentOffsets(dsAux, &aOff));
1159:     PetscCall(PetscDSGetComponentDerivativeOffsets(dsAux, &aOff_x));
1160:     PetscCall(PetscDSGetEvaluationArrays(dsAux, &a, NULL, &a_x));
1161:     auxOnBd = dimAux == dim ? PETSC_TRUE : PETSC_FALSE;
1162:     if (auxOnBd) PetscCall(PetscDSGetTabulation(dsAux, &TAux));
1163:     else PetscCall(PetscDSGetFaceTabulation(dsAux, &TAux));
1164:     PetscCheck(T[0]->Np == TAux[0]->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of tabulation points %" PetscInt_FMT " != %" PetscInt_FMT " number of auxiliary tabulation points", T[0]->Np, TAux[0]->Np);
1165:   }
1166:   PetscCall(PetscDSGetCohesive(ds, fieldI, &isCohesiveFieldI));
1167:   PetscCall(PetscDSGetCohesive(ds, fieldJ, &isCohesiveFieldJ));
1168:   dE  = fgeom->dimEmbed;
1169:   NcI = T[fieldI]->Nc;
1170:   NcJ = T[fieldJ]->Nc;
1171:   NcS = isCohesiveFieldI ? NcI : 2 * NcI;
1172:   NcT = isCohesiveFieldJ ? NcJ : 2 * NcJ;
1173:   if (!isCohesiveFieldI && s == 2) {
1174:     // If we are integrating over a cohesive cell (s = 2) for a non-cohesive fields, we use both sides
1175:     NcS *= 2;
1176:   }
1177:   if (!isCohesiveFieldJ && s == 2) {
1178:     // If we are integrating over a cohesive cell (s = 2) for a non-cohesive fields, we use both sides
1179:     NcT *= 2;
1180:   }
1181:   // The derivatives are constrained to be along the cell, so there are dim, not dE, components, even though
1182:   // the coordinates are in dE dimensions
1183:   PetscCall(PetscArrayzero(g0, NcS * NcT));
1184:   PetscCall(PetscArrayzero(g1, NcS * NcT * dim));
1185:   PetscCall(PetscArrayzero(g2, NcS * NcT * dim));
1186:   PetscCall(PetscArrayzero(g3, NcS * NcT * dim * dim));
1187:   PetscCall(PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights));
1188:   PetscCall(PetscQuadratureGetCellType(quad, &ct));
1189:   PetscCheck(qNc == 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only supports scalar quadrature, not %" PetscInt_FMT " components", qNc);
1190:   for (e = 0; e < Ne; ++e) {
1191:     PetscFEGeom    fegeom, fegeomN[2];
1192:     const PetscInt face[2]  = {fgeom->face[e * 2 + 0][0], fgeom->face[e * 2 + 1][2]};
1193:     const PetscInt ornt[2]  = {fgeom->face[e * 2 + 0][1], fgeom->face[e * 2 + 1][3]};
1194:     const PetscInt cornt[2] = {fgeom->face[e * 2 + 0][3], fgeom->face[e * 2 + 1][1]};

1196:     fegeom.v = x; /* Workspace */
1197:     for (q = 0; q < Nq; ++q) {
1198:       PetscInt  qpt[2];
1199:       PetscReal w;

1201:       PetscCall(PetscDSPermuteQuadPoint(ds, DMPolytopeTypeComposeOrientationInv(ct, cornt[0], ornt[0]), fieldI, q, &qpt[0]));
1202:       PetscCall(PetscDSPermuteQuadPoint(ds, DMPolytopeTypeComposeOrientationInv(ct, ornt[1], cornt[1]), fieldI, q, &qpt[1]));
1203:       PetscCall(PetscFEGeomGetPoint(fgeom, e * 2, q, &quadPoints[q * fgeom->dim], &fegeom));
1204:       PetscCall(PetscFEGeomGetPoint(nbrgeom, e * 2, q, NULL, &fegeomN[0]));
1205:       PetscCall(PetscFEGeomGetPoint(nbrgeom, e * 2 + 1, q, NULL, &fegeomN[1]));
1206:       w = fegeom.detJ[0] * quadWeights[q];
1207:       if (debug > 1 && q < fgeom->numPoints) {
1208:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "  detJ: %g\n", (double)fegeom.detJ[0]));
1209: #if !PetscDefined(USE_COMPLEX)
1210:         PetscCall(DMPrintCellMatrix(e, "invJ", dim, dim, fegeom.invJ));
1211: #endif
1212:       }
1213:       if (debug) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  quad point %" PetscInt_FMT "\n", q));
1214:       if (coefficients) PetscCall(PetscFEEvaluateFieldJets_Hybrid_Internal(dsIn, Nf, 0, q, T, face, qpt, TfIn, &fegeom, fegeomN, &coefficients[cOffset], PetscSafePointerPlusOffset(coefficients_t, cOffset), u, u_x, u_t));
1215:       if (dsAux) PetscCall(PetscFEEvaluateFieldJets_Internal(dsAux, NfAux, auxOnBd ? 0 : face[s], auxOnBd ? q : qpt[s], TAux, &fegeom, &coefficientsAux[cOffsetAux], NULL, a, a_x, NULL));
1216:       if (n0) {
1217:         PetscCall(PetscArrayzero(g0, NcS * NcT));
1218:         for (i = 0; i < n0; ++i) g0_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, u_tshift, fegeom.v, fegeom.n, numConstants, constants, g0);
1219:         for (PetscInt c = 0; c < NcS * NcT; ++c) g0[c] *= w;
1220:       }
1221:       if (n1) {
1222:         PetscCall(PetscArrayzero(g1, NcS * NcT * dim));
1223:         for (i = 0; i < n1; ++i) g1_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, u_tshift, fegeom.v, fegeom.n, numConstants, constants, g1);
1224:         for (PetscInt c = 0; c < NcS * NcT * dim; ++c) g1[c] *= w;
1225:       }
1226:       if (n2) {
1227:         PetscCall(PetscArrayzero(g2, NcS * NcT * dim));
1228:         for (i = 0; i < n2; ++i) g2_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, u_tshift, fegeom.v, fegeom.n, numConstants, constants, g2);
1229:         for (PetscInt c = 0; c < NcS * NcT * dim; ++c) g2[c] *= w;
1230:       }
1231:       if (n3) {
1232:         PetscCall(PetscArrayzero(g3, NcS * NcT * dim * dim));
1233:         for (i = 0; i < n3; ++i) g3_func[i](dE, Nf, NfAux, uOff, uOff_x, u, u_t, u_x, aOff, aOff_x, a, NULL, a_x, t, u_tshift, fegeom.v, fegeom.n, numConstants, constants, g3);
1234:         for (PetscInt c = 0; c < NcS * NcT * dim * dim; ++c) g3[c] *= w;
1235:       }

1237:       if (isCohesiveFieldI) {
1238:         if (isCohesiveFieldJ) {
1239:           //PetscCall(PetscFEUpdateElementMat_Internal(feI, feJ, 0, q, T[fieldI], basisReal, basisDerReal, T[fieldJ], testReal, testDerReal, &fegeom, g0, g1, g2, g3, totDim, offsetI, offsetJ, elemMat + eOffset));
1240:           PetscCall(PetscFEUpdateElementMat_Hybrid_Internal(feI, isCohesiveFieldI, feJ, isCohesiveFieldJ, 0, 0, 0, q, T[fieldI], basisReal, basisDerReal, T[fieldJ], testReal, testDerReal, &fegeom, g0, g1, g2, g3, eOffset, totDim, offsetI, offsetJ, elemMat));
1241:         } else {
1242:           PetscCall(PetscFEUpdateElementMat_Hybrid_Internal(feI, isCohesiveFieldI, feJ, isCohesiveFieldJ, 0, 0, 0, q, T[fieldI], basisReal, basisDerReal, T[fieldJ], testReal, testDerReal, &fegeom, g0, g1, g2, g3, eOffset, totDim, offsetI, offsetJ, elemMat));
1243:           PetscCall(PetscFEUpdateElementMat_Hybrid_Internal(feI, isCohesiveFieldI, feJ, isCohesiveFieldJ, 0, 1, 1, q, T[fieldI], basisReal, basisDerReal, T[fieldJ], testReal, testDerReal, &fegeom, &g0[NcI * NcJ], &g1[NcI * NcJ * dim], &g2[NcI * NcJ * dim], &g3[NcI * NcJ * dim * dim], eOffset, totDim, offsetI, offsetJ, elemMat));
1244:         }
1245:       } else {
1246:         if (s == 2) {
1247:           if (isCohesiveFieldJ) {
1248:             PetscCall(PetscFEUpdateElementMat_Hybrid_Internal(feI, isCohesiveFieldI, feJ, isCohesiveFieldJ, 0, 0, 0, q, T[fieldI], basisReal, basisDerReal, T[fieldJ], testReal, testDerReal, &fegeom, g0, g1, g2, g3, eOffset, totDim, offsetI, offsetJ, elemMat));
1249:             PetscCall(PetscFEUpdateElementMat_Hybrid_Internal(feI, isCohesiveFieldI, feJ, isCohesiveFieldJ, 0, 1, 1, q, T[fieldI], basisReal, basisDerReal, T[fieldJ], testReal, testDerReal, &fegeom, &g0[NcI * NcJ], &g1[NcI * NcJ * dim], &g2[NcI * NcJ * dim], &g3[NcI * NcJ * dim * dim], eOffset, totDim, offsetI, offsetJ, elemMat));
1250:           } else {
1251:             PetscCall(PetscFEUpdateElementMat_Hybrid_Internal(feI, isCohesiveFieldI, feJ, isCohesiveFieldJ, 0, 0, 0, q, T[fieldI], basisReal, basisDerReal, T[fieldJ], testReal, testDerReal, &fegeom, g0, g1, g2, g3, eOffset, totDim, offsetI, offsetJ, elemMat));
1252:             PetscCall(PetscFEUpdateElementMat_Hybrid_Internal(feI, isCohesiveFieldI, feJ, isCohesiveFieldJ, 0, 0, 1, q, T[fieldI], basisReal, basisDerReal, T[fieldJ], testReal, testDerReal, &fegeom, &g0[NcI * NcJ], &g1[NcI * NcJ * dim], &g2[NcI * NcJ * dim], &g3[NcI * NcJ * dim * dim], eOffset, totDim, offsetI, offsetJ, elemMat));
1253:             PetscCall(PetscFEUpdateElementMat_Hybrid_Internal(feI, isCohesiveFieldI, feJ, isCohesiveFieldJ, 0, 1, 0, q, T[fieldI], basisReal, basisDerReal, T[fieldJ], testReal, testDerReal, &fegeom, &g0[NcI * NcJ * 2], &g1[NcI * NcJ * dim * 2], &g2[NcI * NcJ * dim * 2], &g3[NcI * NcJ * dim * dim * 2], eOffset, totDim, offsetI, offsetJ, elemMat));
1254:             PetscCall(PetscFEUpdateElementMat_Hybrid_Internal(feI, isCohesiveFieldI, feJ, isCohesiveFieldJ, 0, 1, 1, q, T[fieldI], basisReal, basisDerReal, T[fieldJ], testReal, testDerReal, &fegeom, &g0[NcI * NcJ * 3], &g1[NcI * NcJ * dim * 3], &g2[NcI * NcJ * dim * 3], &g3[NcI * NcJ * dim * dim * 3], eOffset, totDim, offsetI, offsetJ, elemMat));
1255:           }
1256:         } else
1257:           PetscCall(PetscFEUpdateElementMat_Hybrid_Internal(feI, isCohesiveFieldI, feJ, isCohesiveFieldJ, 0, s, s, q, T[fieldI], basisReal, basisDerReal, T[fieldJ], testReal, testDerReal, &fegeom, g0, g1, g2, g3, eOffset, totDim, offsetI, offsetJ, elemMat));
1258:       }
1259:     }
1260:     if (debug > 1) {
1261:       const PetscInt fS = 0 + (isCohesiveFieldI ? 0 : (s == 2 ? 0 : s * T[fieldI]->Nb));
1262:       const PetscInt fE = T[fieldI]->Nb + (isCohesiveFieldI ? 0 : (s == 2 ? T[fieldI]->Nb : s * T[fieldI]->Nb));
1263:       const PetscInt gS = 0 + (isCohesiveFieldJ ? 0 : (s == 2 ? 0 : s * T[fieldJ]->Nb));
1264:       const PetscInt gE = T[fieldJ]->Nb + (isCohesiveFieldJ ? 0 : (s == 2 ? T[fieldJ]->Nb : s * T[fieldJ]->Nb));

1266:       PetscCall(PetscPrintf(PETSC_COMM_SELF, "Element matrix for fields %" PetscInt_FMT " and %" PetscInt_FMT " s %s totDim %" PetscInt_FMT " offsets (%" PetscInt_FMT ", %" PetscInt_FMT ", %" PetscInt_FMT ")\n", fieldI, fieldJ, s ? (s > 1 ? "Coh" : "Pos") : "Neg", totDim, eOffset, offsetI, offsetJ));
1267:       for (PetscInt f = fS; f < fE; ++f) {
1268:         const PetscInt i = offsetI + f;
1269:         for (PetscInt g = gS; g < gE; ++g) {
1270:           const PetscInt j = offsetJ + g;
1271:           PetscCheck(i < totDim && j < totDim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Fuck up %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT, f, i, g, j);
1272:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "    elemMat[%" PetscInt_FMT ",%" PetscInt_FMT ",%" PetscInt_FMT ",%" PetscInt_FMT "]: %g\n", f / NcI, f % NcI, g / NcJ, g % NcJ, (double)PetscRealPart(elemMat[eOffset + i * totDim + j])));
1273:         }
1274:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1275:       }
1276:     }
1277:     cOffset += totDim;
1278:     cOffsetAux += totDimAux;
1279:     eOffset += PetscSqr(totDim);
1280:   }
1281:   PetscFunctionReturn(PETSC_SUCCESS);
1282: }

1284: static PetscErrorCode PetscFEInitialize_Basic(PetscFE fem)
1285: {
1286:   PetscFunctionBegin;
1287:   fem->ops->setfromoptions          = NULL;
1288:   fem->ops->setup                   = PetscFESetUp_Basic;
1289:   fem->ops->view                    = PetscFEView_Basic;
1290:   fem->ops->destroy                 = PetscFEDestroy_Basic;
1291:   fem->ops->getdimension            = PetscFEGetDimension_Basic;
1292:   fem->ops->computetabulation       = PetscFEComputeTabulation_Basic;
1293:   fem->ops->integrate               = PetscFEIntegrate_Basic;
1294:   fem->ops->integratebd             = PetscFEIntegrateBd_Basic;
1295:   fem->ops->integrateresidual       = PetscFEIntegrateResidual_Basic;
1296:   fem->ops->integratebdresidual     = PetscFEIntegrateBdResidual_Basic;
1297:   fem->ops->integratehybridresidual = PetscFEIntegrateHybridResidual_Basic;
1298:   fem->ops->integratejacobianaction = NULL /* PetscFEIntegrateJacobianAction_Basic */;
1299:   fem->ops->integratejacobian       = PetscFEIntegrateJacobian_Basic;
1300:   fem->ops->integratebdjacobian     = PetscFEIntegrateBdJacobian_Basic;
1301:   fem->ops->integratehybridjacobian = PetscFEIntegrateHybridJacobian_Basic;
1302:   PetscFunctionReturn(PETSC_SUCCESS);
1303: }

1305: /*MC
1306:   PETSCFEBASIC = "basic" - A `PetscFE` object that integrates with basic tiling and no vectorization

1308:   Level: intermediate

1310: .seealso: `PetscFE`, `PetscFEType`, `PetscFECreate()`, `PetscFESetType()`
1311: M*/

1313: PETSC_EXTERN PetscErrorCode PetscFECreate_Basic(PetscFE fem)
1314: {
1315:   PetscFE_Basic *b;

1317:   PetscFunctionBegin;
1319:   PetscCall(PetscNew(&b));
1320:   fem->data = b;

1322:   PetscCall(PetscFEInitialize_Basic(fem));
1323:   PetscFunctionReturn(PETSC_SUCCESS);
1324: }