Actual source code: ex7.c

  1: static char help[] = "Test the PetscDTAltV interface for k-forms (alternating k-linear maps).\n\n";

  3: #include <petscdt.h>

  5: static PetscErrorCode CheckPullback(PetscInt N, PetscInt M, const PetscReal *L, PetscInt k, const PetscReal *w, PetscReal *x, PetscBool verbose, PetscViewer viewer)
  6: {
  7:   PetscInt         Nk, Mk, i, j, l;
  8:   PetscReal       *Lstarw, *Lx, *Lstar, *Lstarwcheck, wLx, Lstarwx;
  9:   PetscReal        diff, diffMat, normMat;
 10:   PetscReal       *walloc   = NULL;
 11:   const PetscReal *ww       = NULL;
 12:   PetscBool        negative = (PetscBool)(k < 0);

 14:   PetscFunctionBegin;
 15:   k = PetscAbsInt(k);
 16:   PetscCall(PetscDTBinomialInt(N, k, &Nk));
 17:   PetscCall(PetscDTBinomialInt(M, k, &Mk));
 18:   if (negative) {
 19:     PetscCall(PetscMalloc1(Mk, &walloc));
 20:     PetscCall(PetscDTAltVStar(M, M - k, 1, w, walloc));
 21:     ww = walloc;
 22:   } else {
 23:     ww = w;
 24:   }
 25:   PetscCall(PetscMalloc2(Nk, &Lstarw, M * k, &Lx));
 26:   PetscCall(PetscMalloc2(Nk * Mk, &Lstar, Nk, &Lstarwcheck));
 27:   PetscCall(PetscDTAltVPullback(N, M, L, negative ? -k : k, w, Lstarw));
 28:   PetscCall(PetscDTAltVPullbackMatrix(N, M, L, negative ? -k : k, Lstar));
 29:   if (negative) {
 30:     PetscReal *sLsw;

 32:     PetscCall(PetscMalloc1(Nk, &sLsw));
 33:     PetscCall(PetscDTAltVStar(N, N - k, 1, Lstarw, sLsw));
 34:     PetscCall(PetscDTAltVApply(N, k, sLsw, x, &Lstarwx));
 35:     PetscCall(PetscFree(sLsw));
 36:   } else {
 37:     PetscCall(PetscDTAltVApply(N, k, Lstarw, x, &Lstarwx));
 38:   }
 39:   for (l = 0; l < k; l++) {
 40:     for (i = 0; i < M; i++) {
 41:       PetscReal sum = 0.;

 43:       for (j = 0; j < N; j++) sum += L[i * N + j] * x[l * N + j];
 44:       Lx[l * M + i] = sum;
 45:     }
 46:   }
 47:   diffMat = 0.;
 48:   normMat = 0.;
 49:   for (i = 0; i < Nk; i++) {
 50:     PetscReal sum = 0.;
 51:     for (j = 0; j < Mk; j++) sum += Lstar[i * Mk + j] * w[j];
 52:     Lstarwcheck[i] = sum;
 53:     diffMat += PetscSqr(PetscAbsReal(Lstarwcheck[i] - Lstarw[i]));
 54:     normMat += PetscSqr(Lstarwcheck[i]) + PetscSqr(Lstarw[i]);
 55:   }
 56:   diffMat = PetscSqrtReal(diffMat);
 57:   normMat = PetscSqrtReal(normMat);
 58:   if (verbose) {
 59:     PetscCall(PetscViewerASCIIPrintf(viewer, "L:\n"));
 60:     PetscCall(PetscViewerASCIIPushTab(viewer));
 61:     if (M * N > 0) PetscCall(PetscRealView(M * N, L, viewer));
 62:     PetscCall(PetscViewerASCIIPopTab(viewer));

 64:     PetscCall(PetscViewerASCIIPrintf(viewer, "L*:\n"));
 65:     PetscCall(PetscViewerASCIIPushTab(viewer));
 66:     if (Nk * Mk > 0) PetscCall(PetscRealView(Nk * Mk, Lstar, viewer));
 67:     PetscCall(PetscViewerASCIIPopTab(viewer));

 69:     PetscCall(PetscViewerASCIIPrintf(viewer, "L*w:\n"));
 70:     PetscCall(PetscViewerASCIIPushTab(viewer));
 71:     if (Nk > 0) PetscCall(PetscRealView(Nk, Lstarw, viewer));
 72:     PetscCall(PetscViewerASCIIPopTab(viewer));
 73:   }
 74:   PetscCall(PetscDTAltVApply(M, k, ww, Lx, &wLx));
 75:   diff = PetscAbsReal(wLx - Lstarwx);
 76:   PetscCheck(diff <= 10. * PETSC_SMALL * (PetscAbsReal(wLx) + PetscAbsReal(Lstarwx)), PETSC_COMM_WORLD, PETSC_ERR_PLIB, "pullback check: pullback does not commute with application: w(Lx)(%g) != (L* w)(x)(%g)", (double)wLx, (double)Lstarwx);
 77:   PetscCheck(diffMat <= PETSC_SMALL * normMat, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "pullback check: pullback matrix does match matrix-free result");
 78:   PetscCall(PetscFree2(Lstar, Lstarwcheck));
 79:   PetscCall(PetscFree2(Lstarw, Lx));
 80:   PetscCall(PetscFree(walloc));
 81:   PetscFunctionReturn(PETSC_SUCCESS);
 82: }

 84: int main(int argc, char **argv)
 85: {
 86:   PetscInt    i, numTests = 5, n[5] = {0, 1, 2, 3, 4};
 87:   PetscBool   verbose = PETSC_FALSE;
 88:   PetscRandom rand;
 89:   PetscViewer viewer;

 91:   PetscFunctionBeginUser;
 92:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 93:   PetscOptionsBegin(PETSC_COMM_WORLD, "", "Options for exterior algebra tests", "none");
 94:   PetscCall(PetscOptionsIntArray("-N", "Up to 5 vector space dimensions to test", "ex7.c", n, &numTests, NULL));
 95:   PetscCall(PetscOptionsBool("-verbose", "Verbose test output", "ex7.c", verbose, &verbose, NULL));
 96:   PetscOptionsEnd();
 97:   PetscCall(PetscRandomCreate(PETSC_COMM_SELF, &rand));
 98:   PetscCall(PetscRandomSetInterval(rand, -1., 1.));
 99:   PetscCall(PetscRandomSetFromOptions(rand));
100:   if (!numTests) numTests = 5;
101:   viewer = PETSC_VIEWER_STDOUT_(PETSC_COMM_WORLD);
102:   for (i = 0; i < numTests; i++) {
103:     PetscInt k, N = n[i];

105:     if (verbose) PetscCall(PetscViewerASCIIPrintf(viewer, "N = %" PetscInt_FMT ":\n", N));
106:     PetscCall(PetscViewerASCIIPushTab(viewer));

108:     if (verbose) {
109:       PetscInt *perm;
110:       PetscInt  fac = 1;

112:       PetscCall(PetscMalloc1(N, &perm));

114:       for (k = 1; k <= N; k++) fac *= k;
115:       PetscCall(PetscViewerASCIIPrintf(viewer, "Permutations of %" PetscInt_FMT ":\n", N));
116:       PetscCall(PetscViewerASCIIPushTab(viewer));
117:       for (k = 0; k < fac; k++) {
118:         PetscBool isOdd, isOddCheck;
119:         PetscInt  j, kCheck;

121:         PetscCall(PetscDTEnumPerm(N, k, perm, &isOdd));
122:         PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ":", k));
123:         for (j = 0; j < N; j++) PetscCall(PetscPrintf(PETSC_COMM_WORLD, " %" PetscInt_FMT, perm[j]));
124:         PetscCall(PetscPrintf(PETSC_COMM_WORLD, ", %s\n", isOdd ? "odd" : "even"));
125:         PetscCall(PetscDTPermIndex(N, perm, &kCheck, &isOddCheck));
126:         PetscCheck(kCheck == k && isOddCheck == isOdd, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscDTEnumPerm / PetscDTPermIndex mismatch for (%" PetscInt_FMT ", %" PetscInt_FMT ")", N, k);
127:       }
128:       PetscCall(PetscViewerASCIIPopTab(viewer));
129:       PetscCall(PetscFree(perm));
130:     }
131:     for (k = 0; k <= N; k++) {
132:       PetscInt   j, Nk, M;
133:       PetscReal *w, *v, wv;
134:       PetscInt  *subset;

136:       PetscCall(PetscDTBinomialInt(N, k, &Nk));
137:       if (verbose) PetscCall(PetscViewerASCIIPrintf(viewer, "k = %" PetscInt_FMT ":\n", k));
138:       PetscCall(PetscViewerASCIIPushTab(viewer));
139:       if (verbose) PetscCall(PetscViewerASCIIPrintf(viewer, "(%" PetscInt_FMT " choose %" PetscInt_FMT "): %" PetscInt_FMT "\n", N, k, Nk));

141:       /* Test subset and complement enumeration */
142:       PetscCall(PetscMalloc1(N, &subset));
143:       PetscCall(PetscViewerASCIIPushTab(viewer));
144:       for (j = 0; j < Nk; j++) {
145:         PetscBool isOdd, isOddCheck;
146:         PetscInt  jCheck, kCheck;

148:         PetscCall(PetscDTEnumSplit(N, k, j, subset, &isOdd));
149:         PetscCall(PetscDTPermIndex(N, subset, &kCheck, &isOddCheck));
150:         PetscCheck(isOddCheck == isOdd, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscDTEnumSplit sign does not mmatch PetscDTPermIndex sign");
151:         if (verbose) {
152:           PetscCall(PetscViewerASCIIPrintf(viewer, "subset %" PetscInt_FMT ":", j));
153:           for (PetscInt l = 0; l < k; l++) PetscCall(PetscPrintf(PETSC_COMM_WORLD, " %" PetscInt_FMT, subset[l]));
154:           PetscCall(PetscPrintf(PETSC_COMM_WORLD, " |"));
155:           for (PetscInt l = k; l < N; l++) PetscCall(PetscPrintf(PETSC_COMM_WORLD, " %" PetscInt_FMT, subset[l]));
156:           PetscCall(PetscPrintf(PETSC_COMM_WORLD, ", %s\n", isOdd ? "odd" : "even"));
157:         }
158:         PetscCall(PetscDTSubsetIndex(N, k, subset, &jCheck));
159:         PetscCheck(jCheck == j, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "jCheck (%" PetscInt_FMT ") != j (%" PetscInt_FMT ")", jCheck, j);
160:       }
161:       PetscCall(PetscViewerASCIIPopTab(viewer));
162:       PetscCall(PetscFree(subset));

164:       /* Make a random k form */
165:       PetscCall(PetscMalloc1(Nk, &w));
166:       for (j = 0; j < Nk; j++) PetscCall(PetscRandomGetValueReal(rand, &w[j]));
167:       /* Make a set of random vectors */
168:       PetscCall(PetscMalloc1(N * k, &v));
169:       for (j = 0; j < N * k; j++) PetscCall(PetscRandomGetValueReal(rand, &v[j]));

171:       PetscCall(PetscDTAltVApply(N, k, w, v, &wv));

173:       if (verbose) {
174:         PetscCall(PetscViewerASCIIPrintf(viewer, "w:\n"));
175:         PetscCall(PetscViewerASCIIPushTab(viewer));
176:         if (Nk) PetscCall(PetscRealView(Nk, w, viewer));
177:         PetscCall(PetscViewerASCIIPopTab(viewer));
178:         PetscCall(PetscViewerASCIIPrintf(viewer, "v:\n"));
179:         PetscCall(PetscViewerASCIIPushTab(viewer));
180:         if (N * k > 0) PetscCall(PetscRealView(N * k, v, viewer));
181:         PetscCall(PetscViewerASCIIPopTab(viewer));
182:         PetscCall(PetscViewerASCIIPrintf(viewer, "w(v): %g\n", (double)wv));
183:       }

185:       /* sanity checks */
186:       if (k == 1) { /* 1-forms are functionals (dot products) */
187:         PetscInt  l;
188:         PetscReal wvcheck = 0.;
189:         PetscReal diff;

191:         for (l = 0; l < N; l++) wvcheck += w[l] * v[l];
192:         diff = PetscSqrtReal(PetscSqr(wvcheck - wv));
193:         PetscCheck(diff < PETSC_SMALL * (PetscAbsReal(wv) + PetscAbsReal(wvcheck)), PETSC_COMM_WORLD, PETSC_ERR_PLIB, "1-form / dot product equivalence: wvcheck (%g) != wv (%g)", (double)wvcheck, (double)wv);
194:       }
195:       if (k == N && N < 5) { /* n-forms are scaled determinants */
196:         PetscReal det, wvcheck, diff;

198:         switch (k) {
199:         case 0:
200:           det = 1.;
201:           break;
202:         case 1:
203:           det = v[0];
204:           break;
205:         case 2:
206:           det = v[0] * v[3] - v[1] * v[2];
207:           break;
208:         case 3:
209:           det = v[0] * (v[4] * v[8] - v[5] * v[7]) + v[1] * (v[5] * v[6] - v[3] * v[8]) + v[2] * (v[3] * v[7] - v[4] * v[6]);
210:           break;
211:         case 4:
212:           det = v[0] * (v[5] * (v[10] * v[15] - v[11] * v[14]) + v[6] * (v[11] * v[13] - v[9] * v[15]) + v[7] * (v[9] * v[14] - v[10] * v[13])) - v[1] * (v[4] * (v[10] * v[15] - v[11] * v[14]) + v[6] * (v[11] * v[12] - v[8] * v[15]) + v[7] * (v[8] * v[14] - v[10] * v[12])) + v[2] * (v[4] * (v[9] * v[15] - v[11] * v[13]) + v[5] * (v[11] * v[12] - v[8] * v[15]) + v[7] * (v[8] * v[13] - v[9] * v[12])) - v[3] * (v[4] * (v[9] * v[14] - v[10] * v[13]) + v[5] * (v[10] * v[12] - v[8] * v[14]) + v[6] * (v[8] * v[13] - v[9] * v[12]));
213:           break;
214:         default:
215:           SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "invalid k");
216:         }
217:         wvcheck = det * w[0];
218:         diff    = PetscSqrtReal(PetscSqr(wvcheck - wv));
219:         PetscCheck(diff < PETSC_SMALL * (PetscAbsReal(wv) + PetscAbsReal(wvcheck)), PETSC_COMM_WORLD, PETSC_ERR_PLIB, "n-form / determinant equivalence: wvcheck (%g) != wv (%g) %g", (double)wvcheck, (double)wv, (double)diff);
220:       }
221:       if (k > 0) { /* k-forms are linear in each component */
222:         PetscReal  alpha;
223:         PetscReal *x, *axv, wx, waxv, waxvcheck;
224:         PetscReal  diff;
225:         PetscReal  rj;
226:         PetscInt   l;

228:         PetscCall(PetscMalloc2(N * k, &x, N * k, &axv));
229:         PetscCall(PetscRandomGetValueReal(rand, &alpha));
230:         PetscCall(PetscRandomSetInterval(rand, 0, k));
231:         PetscCall(PetscRandomGetValueReal(rand, &rj));
232:         j = (PetscInt)rj;
233:         PetscCall(PetscRandomSetInterval(rand, -1., 1.));
234:         for (l = 0; l < N * k; l++) x[l] = v[l];
235:         for (l = 0; l < N * k; l++) axv[l] = v[l];
236:         for (l = 0; l < N; l++) {
237:           PetscReal val;

239:           PetscCall(PetscRandomGetValueReal(rand, &val));
240:           x[j * N + l] = val;
241:           axv[j * N + l] += alpha * val;
242:         }
243:         PetscCall(PetscDTAltVApply(N, k, w, x, &wx));
244:         PetscCall(PetscDTAltVApply(N, k, w, axv, &waxv));
245:         waxvcheck = alpha * wx + wv;
246:         diff      = waxv - waxvcheck;
247:         PetscCheck(PetscAbsReal(diff) <= 10. * PETSC_SMALL * (PetscAbsReal(waxv) + PetscAbsReal(waxvcheck)), PETSC_COMM_WORLD, PETSC_ERR_PLIB, "linearity check: component %" PetscInt_FMT ", waxvcheck (%g) != waxv (%g)", j, (double)waxvcheck, (double)waxv);
248:         PetscCall(PetscFree2(x, axv));
249:       }
250:       if (k > 1) { /* k-forms are antisymmetric */
251:         PetscReal rj, rl, *swapv, wswapv, diff;
252:         PetscInt  l, m;

254:         PetscCall(PetscRandomSetInterval(rand, 0, k));
255:         PetscCall(PetscRandomGetValueReal(rand, &rj));
256:         j = (PetscInt)rj;
257:         l = j;
258:         while (l == j) {
259:           PetscCall(PetscRandomGetValueReal(rand, &rl));
260:           l = (PetscInt)rl;
261:         }
262:         PetscCall(PetscRandomSetInterval(rand, -1., 1.));
263:         PetscCall(PetscMalloc1(N * k, &swapv));
264:         for (m = 0; m < N * k; m++) swapv[m] = v[m];
265:         for (m = 0; m < N; m++) {
266:           swapv[j * N + m] = v[l * N + m];
267:           swapv[l * N + m] = v[j * N + m];
268:         }
269:         PetscCall(PetscDTAltVApply(N, k, w, swapv, &wswapv));
270:         diff = PetscAbsReal(wswapv + wv);
271:         PetscCheck(diff <= PETSC_SMALL * (PetscAbsReal(wswapv) + PetscAbsReal(wv)), PETSC_COMM_WORLD, PETSC_ERR_PLIB, "antisymmetry check: components %" PetscInt_FMT " & %" PetscInt_FMT ", wswapv (%g) != -wv (%g)", j, l, (double)wswapv, (double)wv);
272:         PetscCall(PetscFree(swapv));
273:       }
274:       for (j = 0; j <= k && j + k <= N; j++) { /* wedge product */
275:         PetscInt   Nj, Njk, l, JKj;
276:         PetscReal *u, *uWw, *uWwcheck, *uWwmat, *x, *xsplit, uWwx, uWwxcheck, diff, norm;
277:         PetscInt  *split;

279:         if (verbose) PetscCall(PetscViewerASCIIPrintf(viewer, "wedge j = %" PetscInt_FMT ":\n", j));
280:         PetscCall(PetscViewerASCIIPushTab(viewer));
281:         PetscCall(PetscDTBinomialInt(N, j, &Nj));
282:         PetscCall(PetscDTBinomialInt(N, j + k, &Njk));
283:         PetscCall(PetscMalloc4(Nj, &u, Njk, &uWw, N * (j + k), &x, N * (j + k), &xsplit));
284:         PetscCall(PetscMalloc1(j + k, &split));
285:         for (l = 0; l < Nj; l++) PetscCall(PetscRandomGetValueReal(rand, &u[l]));
286:         for (l = 0; l < N * (j + k); l++) PetscCall(PetscRandomGetValueReal(rand, &x[l]));
287:         PetscCall(PetscDTAltVWedge(N, j, k, u, w, uWw));
288:         PetscCall(PetscDTAltVApply(N, j + k, uWw, x, &uWwx));
289:         if (verbose) {
290:           PetscCall(PetscViewerASCIIPrintf(viewer, "u:\n"));
291:           PetscCall(PetscViewerASCIIPushTab(viewer));
292:           if (Nj) PetscCall(PetscRealView(Nj, u, viewer));
293:           PetscCall(PetscViewerASCIIPopTab(viewer));
294:           PetscCall(PetscViewerASCIIPrintf(viewer, "u wedge w:\n"));
295:           PetscCall(PetscViewerASCIIPushTab(viewer));
296:           if (Njk) PetscCall(PetscRealView(Njk, uWw, viewer));
297:           PetscCall(PetscViewerASCIIPopTab(viewer));
298:           PetscCall(PetscViewerASCIIPrintf(viewer, "x:\n"));
299:           PetscCall(PetscViewerASCIIPushTab(viewer));
300:           if (N * (j + k) > 0) PetscCall(PetscRealView(N * (j + k), x, viewer));
301:           PetscCall(PetscViewerASCIIPopTab(viewer));
302:           PetscCall(PetscViewerASCIIPrintf(viewer, "u wedge w(x): %g\n", (double)uWwx));
303:         }
304:         /* verify wedge formula */
305:         uWwxcheck = 0.;
306:         PetscCall(PetscDTBinomialInt(j + k, j, &JKj));
307:         for (l = 0; l < JKj; l++) {
308:           PetscBool isOdd;
309:           PetscReal ux, wx;
310:           PetscInt  m;

312:           PetscCall(PetscDTEnumSplit(j + k, j, l, split, &isOdd));
313:           for (m = 0; m < j + k; m++) {
314:             for (PetscInt p = 0; p < N; p++) xsplit[m * N + p] = x[split[m] * N + p];
315:           }
316:           PetscCall(PetscDTAltVApply(N, j, u, xsplit, &ux));
317:           PetscCall(PetscDTAltVApply(N, k, w, PetscSafePointerPlusOffset(xsplit, j * N), &wx));
318:           uWwxcheck += isOdd ? -(ux * wx) : (ux * wx);
319:         }
320:         diff = PetscAbsReal(uWwx - uWwxcheck);
321:         PetscCheck(diff <= 10. * PETSC_SMALL * (PetscAbsReal(uWwx) + PetscAbsReal(uWwxcheck)), PETSC_COMM_WORLD, PETSC_ERR_PLIB, "wedge check: forms %" PetscInt_FMT " & %" PetscInt_FMT ", uWwxcheck (%g) != uWwx (%g)", j, k, (double)uWwxcheck, (double)uWwx);
322:         PetscCall(PetscFree(split));
323:         PetscCall(PetscMalloc2(Nk * Njk, &uWwmat, Njk, &uWwcheck));
324:         PetscCall(PetscDTAltVWedgeMatrix(N, j, k, u, uWwmat));
325:         if (verbose) {
326:           PetscCall(PetscViewerASCIIPrintf(viewer, "(u wedge):\n"));
327:           PetscCall(PetscViewerASCIIPushTab(viewer));
328:           if (Nk * Njk > 0) PetscCall(PetscRealView(Nk * Njk, uWwmat, viewer));
329:           PetscCall(PetscViewerASCIIPopTab(viewer));
330:         }
331:         diff = 0.;
332:         norm = 0.;
333:         for (l = 0; l < Njk; l++) {
334:           PetscReal sum = 0.;

336:           for (PetscInt m = 0; m < Nk; m++) sum += uWwmat[l * Nk + m] * w[m];
337:           uWwcheck[l] = sum;
338:           diff += PetscSqr(uWwcheck[l] - uWw[l]);
339:           norm += PetscSqr(uWwcheck[l]) + PetscSqr(uWw[l]);
340:         }
341:         diff = PetscSqrtReal(diff);
342:         norm = PetscSqrtReal(norm);
343:         PetscCheck(diff <= PETSC_SMALL * norm, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "wedge matrix check: wedge matrix application does not match wedge direct application");
344:         PetscCall(PetscFree2(uWwmat, uWwcheck));
345:         PetscCall(PetscFree4(u, uWw, x, xsplit));
346:         PetscCall(PetscViewerASCIIPopTab(viewer));
347:       }
348:       for (M = PetscMax(1, k); M <= N; M++) { /* pullback */
349:         PetscReal *L, *u, *x;
350:         PetscInt   Mk, l;

352:         PetscCall(PetscDTBinomialInt(M, k, &Mk));
353:         PetscCall(PetscMalloc3(M * N, &L, Mk, &u, M * k, &x));
354:         for (l = 0; l < M * N; l++) PetscCall(PetscRandomGetValueReal(rand, &L[l]));
355:         for (l = 0; l < Mk; l++) PetscCall(PetscRandomGetValueReal(rand, &u[l]));
356:         for (l = 0; l < M * k; l++) PetscCall(PetscRandomGetValueReal(rand, &x[l]));
357:         if (verbose) PetscCall(PetscViewerASCIIPrintf(viewer, "pullback M = %" PetscInt_FMT ":\n", M));
358:         PetscCall(PetscViewerASCIIPushTab(viewer));
359:         PetscCall(CheckPullback(M, N, L, k, w, x, verbose, viewer));
360:         if (M != N) PetscCall(CheckPullback(N, M, L, k, u, v, PETSC_FALSE, viewer));
361:         PetscCall(PetscViewerASCIIPopTab(viewer));
362:         if ((k % N) && N > 1) {
363:           if (verbose) PetscCall(PetscViewerASCIIPrintf(viewer, "negative pullback M = %" PetscInt_FMT ":\n", M));
364:           PetscCall(PetscViewerASCIIPushTab(viewer));
365:           PetscCall(CheckPullback(M, N, L, -k, w, x, verbose, viewer));
366:           if (M != N) PetscCall(CheckPullback(N, M, L, -k, u, v, PETSC_FALSE, viewer));
367:           PetscCall(PetscViewerASCIIPopTab(viewer));
368:         }
369:         PetscCall(PetscFree3(L, u, x));
370:       }
371:       if (k > 0) { /* Interior */
372:         PetscInt   Nkm, l, m;
373:         PetscReal *wIntv0, *wIntv0check, wvcheck, diff, diffMat, normMat;
374:         PetscReal *intv0mat, *matcheck;
375:         PetscInt (*indices)[3];

377:         PetscCall(PetscDTBinomialInt(N, k - 1, &Nkm));
378:         PetscCall(PetscMalloc5(Nkm, &wIntv0, Nkm, &wIntv0check, Nk * Nkm, &intv0mat, Nk * Nkm, &matcheck, Nk * k, &indices));
379:         PetscCall(PetscDTAltVInterior(N, k, w, v, wIntv0));
380:         PetscCall(PetscDTAltVInteriorMatrix(N, k, v, intv0mat));
381:         PetscCall(PetscDTAltVInteriorPattern(N, k, indices));
382:         if (verbose) {
383:           PetscCall(PetscViewerASCIIPrintf(viewer, "interior product matrix pattern:\n"));
384:           PetscCall(PetscViewerASCIIPushTab(viewer));
385:           for (l = 0; l < Nk * k; l++) {
386:             PetscInt row = indices[l][0];
387:             PetscInt col = indices[l][1];
388:             PetscInt x   = indices[l][2];

390:             PetscCall(PetscViewerASCIIPrintf(viewer, "intV[%" PetscInt_FMT ",%" PetscInt_FMT "] = %sV[%" PetscInt_FMT "]\n", row, col, x < 0 ? "-" : " ", x < 0 ? -(x + 1) : x));
391:           }
392:           PetscCall(PetscViewerASCIIPopTab(viewer));
393:         }
394:         for (l = 0; l < Nkm * Nk; l++) matcheck[l] = 0.;
395:         for (l = 0; l < Nk * k; l++) {
396:           PetscInt row = indices[l][0];
397:           PetscInt col = indices[l][1];
398:           PetscInt x   = indices[l][2];

400:           if (x < 0) {
401:             matcheck[row * Nk + col] = -v[-(x + 1)];
402:           } else {
403:             matcheck[row * Nk + col] = v[x];
404:           }
405:         }
406:         diffMat = 0.;
407:         normMat = 0.;
408:         for (l = 0; l < Nkm * Nk; l++) {
409:           diffMat += PetscSqr(PetscAbsReal(matcheck[l] - intv0mat[l]));
410:           normMat += PetscSqr(matcheck[l]) + PetscSqr(intv0mat[l]);
411:         }
412:         diffMat = PetscSqrtReal(diffMat);
413:         normMat = PetscSqrtReal(normMat);
414:         PetscCheck(diffMat <= PETSC_SMALL * normMat, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Interior product check: matrix pattern does not match matrix");
415:         diffMat = 0.;
416:         normMat = 0.;
417:         for (l = 0; l < Nkm; l++) {
418:           PetscReal sum = 0.;

420:           for (m = 0; m < Nk; m++) sum += intv0mat[l * Nk + m] * w[m];
421:           wIntv0check[l] = sum;

423:           diffMat += PetscSqr(PetscAbsReal(wIntv0check[l] - wIntv0[l]));
424:           normMat += PetscSqr(wIntv0check[l]) + PetscSqr(wIntv0[l]);
425:         }
426:         diffMat = PetscSqrtReal(diffMat);
427:         normMat = PetscSqrtReal(normMat);
428:         PetscCheck(diffMat <= PETSC_SMALL * normMat, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Interior product check: application does not match matrix");
429:         if (verbose) {
430:           PetscCall(PetscViewerASCIIPrintf(viewer, "(w int v_0):\n"));
431:           PetscCall(PetscViewerASCIIPushTab(viewer));
432:           if (Nkm) PetscCall(PetscRealView(Nkm, wIntv0, viewer));
433:           PetscCall(PetscViewerASCIIPopTab(viewer));

435:           PetscCall(PetscViewerASCIIPrintf(viewer, "(int v_0):\n"));
436:           PetscCall(PetscViewerASCIIPushTab(viewer));
437:           if (Nk * Nkm > 0) PetscCall(PetscRealView(Nk * Nkm, intv0mat, viewer));
438:           PetscCall(PetscViewerASCIIPopTab(viewer));
439:         }
440:         PetscCall(PetscDTAltVApply(N, k - 1, wIntv0, &v[N], &wvcheck));
441:         diff = PetscSqrtReal(PetscSqr(wvcheck - wv));
442:         PetscCheck(diff < PETSC_SMALL * (PetscAbsReal(wv) + PetscAbsReal(wvcheck)), PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Interior product check: (w Int v0)(v_rem) (%g) != w(v) (%g)", (double)wvcheck, (double)wv);
443:         PetscCall(PetscFree5(wIntv0, wIntv0check, intv0mat, matcheck, indices));
444:       }
445:       if (k >= N - k) { /* Hodge star */
446:         PetscReal *u, *starw, *starstarw, wu, starwdotu;
447:         PetscReal  diff, norm;
448:         PetscBool  isOdd;

450:         isOdd = (PetscBool)((k * (N - k)) & 1);
451:         PetscCall(PetscMalloc3(Nk, &u, Nk, &starw, Nk, &starstarw));
452:         PetscCall(PetscDTAltVStar(N, k, 1, w, starw));
453:         PetscCall(PetscDTAltVStar(N, N - k, 1, starw, starstarw));
454:         if (verbose) {
455:           PetscCall(PetscViewerASCIIPrintf(viewer, "star w:\n"));
456:           PetscCall(PetscViewerASCIIPushTab(viewer));
457:           if (Nk) PetscCall(PetscRealView(Nk, starw, viewer));
458:           PetscCall(PetscViewerASCIIPopTab(viewer));

460:           PetscCall(PetscViewerASCIIPrintf(viewer, "star star w:\n"));
461:           PetscCall(PetscViewerASCIIPushTab(viewer));
462:           if (Nk) PetscCall(PetscRealView(Nk, starstarw, viewer));
463:           PetscCall(PetscViewerASCIIPopTab(viewer));
464:         }
465:         for (PetscInt l = 0; l < Nk; l++) PetscCall(PetscRandomGetValueReal(rand, &u[l]));
466:         PetscCall(PetscDTAltVWedge(N, k, N - k, w, u, &wu));
467:         starwdotu = 0.;
468:         for (PetscInt l = 0; l < Nk; l++) starwdotu += starw[l] * u[l];
469:         diff = PetscAbsReal(wu - starwdotu);
470:         PetscCheck(diff <= PETSC_SMALL * (PetscAbsReal(wu) + PetscAbsReal(starwdotu)), PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Hodge star check: (star w, u) (%g) != (w wedge u) (%g)", (double)starwdotu, (double)wu);

472:         diff = 0.;
473:         norm = 0.;
474:         for (PetscInt l = 0; l < Nk; l++) {
475:           diff += PetscSqr(w[l] - (isOdd ? -starstarw[l] : starstarw[l]));
476:           norm += PetscSqr(w[l]) + PetscSqr(starstarw[l]);
477:         }
478:         diff = PetscSqrtReal(diff);
479:         norm = PetscSqrtReal(norm);
480:         PetscCheck(diff <= PETSC_SMALL * norm, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Hodge star check: star(star(w)) != (-1)^(N*(N-k)) w");
481:         PetscCall(PetscFree3(u, starw, starstarw));
482:       }
483:       PetscCall(PetscFree(v));
484:       PetscCall(PetscFree(w));
485:       PetscCall(PetscViewerASCIIPopTab(viewer));
486:     }
487:     PetscCall(PetscViewerASCIIPopTab(viewer));
488:   }
489:   PetscCall(PetscRandomDestroy(&rand));
490:   PetscCall(PetscFinalize());
491:   return 0;
492: }

494: /*TEST
495:   test:
496:     suffix: 1234
497:     args: -verbose
498:   test:
499:     suffix: 56
500:     args: -N 5,6
501:     output_file: output/empty.out
502: TEST*/