Actual source code: dmplexsnes.c
petsc-dev 2014-02-02
1: #include <petscdmplex.h> /*I "petscdmplex.h" I*/
2: #include <petscsnes.h> /*I "petscsnes.h" I*/
3: #include <petsc-private/petscimpl.h>
7: PetscErrorCode DMInterpolationCreate(MPI_Comm comm, DMInterpolationInfo *ctx)
8: {
13: PetscMalloc(sizeof(struct _DMInterpolationInfo), ctx);
15: (*ctx)->comm = comm;
16: (*ctx)->dim = -1;
17: (*ctx)->nInput = 0;
18: (*ctx)->points = NULL;
19: (*ctx)->cells = NULL;
20: (*ctx)->n = -1;
21: (*ctx)->coords = NULL;
22: return(0);
23: }
27: PetscErrorCode DMInterpolationSetDim(DMInterpolationInfo ctx, PetscInt dim)
28: {
30: if ((dim < 1) || (dim > 3)) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension for points: %d", dim);
31: ctx->dim = dim;
32: return(0);
33: }
37: PetscErrorCode DMInterpolationGetDim(DMInterpolationInfo ctx, PetscInt *dim)
38: {
41: *dim = ctx->dim;
42: return(0);
43: }
47: PetscErrorCode DMInterpolationSetDof(DMInterpolationInfo ctx, PetscInt dof)
48: {
50: if (dof < 1) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of components: %d", dof);
51: ctx->dof = dof;
52: return(0);
53: }
57: PetscErrorCode DMInterpolationGetDof(DMInterpolationInfo ctx, PetscInt *dof)
58: {
61: *dof = ctx->dof;
62: return(0);
63: }
67: PetscErrorCode DMInterpolationAddPoints(DMInterpolationInfo ctx, PetscInt n, PetscReal points[])
68: {
72: if (ctx->dim < 0) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set");
73: if (ctx->points) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "Cannot add points multiple times yet");
74: ctx->nInput = n;
76: PetscMalloc1(n*ctx->dim, &ctx->points);
77: PetscMemcpy(ctx->points, points, n*ctx->dim * sizeof(PetscReal));
78: return(0);
79: }
83: PetscErrorCode DMInterpolationSetUp(DMInterpolationInfo ctx, DM dm, PetscBool redundantPoints)
84: {
85: MPI_Comm comm = ctx->comm;
86: PetscScalar *a;
87: PetscInt p, q, i;
88: PetscMPIInt rank, size;
90: Vec pointVec;
91: IS cellIS;
92: PetscLayout layout;
93: PetscReal *globalPoints;
94: PetscScalar *globalPointsScalar;
95: const PetscInt *ranges;
96: PetscMPIInt *counts, *displs;
97: const PetscInt *foundCells;
98: PetscMPIInt *foundProcs, *globalProcs;
99: PetscInt n, N;
103: MPI_Comm_size(comm, &size);
104: MPI_Comm_rank(comm, &rank);
105: if (ctx->dim < 0) SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set");
106: /* Locate points */
107: n = ctx->nInput;
108: if (!redundantPoints) {
109: PetscLayoutCreate(comm, &layout);
110: PetscLayoutSetBlockSize(layout, 1);
111: PetscLayoutSetLocalSize(layout, n);
112: PetscLayoutSetUp(layout);
113: PetscLayoutGetSize(layout, &N);
114: /* Communicate all points to all processes */
115: PetscMalloc3(N*ctx->dim,&globalPoints,size,&counts,size,&displs);
116: PetscLayoutGetRanges(layout, &ranges);
117: for (p = 0; p < size; ++p) {
118: counts[p] = (ranges[p+1] - ranges[p])*ctx->dim;
119: displs[p] = ranges[p]*ctx->dim;
120: }
121: MPI_Allgatherv(ctx->points, n*ctx->dim, MPIU_REAL, globalPoints, counts, displs, MPIU_REAL, comm);
122: } else {
123: N = n;
125: globalPoints = ctx->points;
126: }
127: #if 0
128: PetscMalloc3(N,&foundCells,N,&foundProcs,N,&globalProcs);
129: /* foundCells[p] = m->locatePoint(&globalPoints[p*ctx->dim]); */
130: #else
131: #if defined(PETSC_USE_COMPLEX)
132: PetscMalloc1(N,&globalPointsScalar);
133: for (i=0; i<N; i++) globalPointsScalar[i] = globalPoints[i];
134: #else
135: globalPointsScalar = globalPoints;
136: #endif
137: VecCreateSeqWithArray(PETSC_COMM_SELF, ctx->dim, N*ctx->dim, globalPointsScalar, &pointVec);
138: PetscMalloc2(N,&foundProcs,N,&globalProcs);
139: DMLocatePoints(dm, pointVec, &cellIS);
140: ISGetIndices(cellIS, &foundCells);
141: #endif
142: for (p = 0; p < N; ++p) {
143: if (foundCells[p] >= 0) foundProcs[p] = rank;
144: else foundProcs[p] = size;
145: }
146: /* Let the lowest rank process own each point */
147: MPI_Allreduce(foundProcs, globalProcs, N, MPI_INT, MPI_MIN, comm);
148: ctx->n = 0;
149: for (p = 0; p < N; ++p) {
150: if (globalProcs[p] == size) SETERRQ4(comm, PETSC_ERR_PLIB, "Point %d: %g %g %g not located in mesh", p, globalPoints[p*ctx->dim+0], ctx->dim > 1 ? globalPoints[p*ctx->dim+1] : 0.0, ctx->dim > 2 ? globalPoints[p*ctx->dim+2] : 0.0);
151: else if (globalProcs[p] == rank) ctx->n++;
152: }
153: /* Create coordinates vector and array of owned cells */
154: PetscMalloc1(ctx->n, &ctx->cells);
155: VecCreate(comm, &ctx->coords);
156: VecSetSizes(ctx->coords, ctx->n*ctx->dim, PETSC_DECIDE);
157: VecSetBlockSize(ctx->coords, ctx->dim);
158: VecSetType(ctx->coords,VECSTANDARD);
159: VecGetArray(ctx->coords, &a);
160: for (p = 0, q = 0, i = 0; p < N; ++p) {
161: if (globalProcs[p] == rank) {
162: PetscInt d;
164: for (d = 0; d < ctx->dim; ++d, ++i) a[i] = globalPoints[p*ctx->dim+d];
165: ctx->cells[q++] = foundCells[p];
166: }
167: }
168: VecRestoreArray(ctx->coords, &a);
169: #if 0
170: PetscFree3(foundCells,foundProcs,globalProcs);
171: #else
172: PetscFree2(foundProcs,globalProcs);
173: ISRestoreIndices(cellIS, &foundCells);
174: ISDestroy(&cellIS);
175: VecDestroy(&pointVec);
176: #endif
177: if ((void*)globalPointsScalar != (void*)globalPoints) {PetscFree(globalPointsScalar);}
178: if (!redundantPoints) {
179: PetscFree3(globalPoints,counts,displs);
180: PetscLayoutDestroy(&layout);
181: }
182: return(0);
183: }
187: PetscErrorCode DMInterpolationGetCoordinates(DMInterpolationInfo ctx, Vec *coordinates)
188: {
191: if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
192: *coordinates = ctx->coords;
193: return(0);
194: }
198: PetscErrorCode DMInterpolationGetVector(DMInterpolationInfo ctx, Vec *v)
199: {
204: if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
205: VecCreate(ctx->comm, v);
206: VecSetSizes(*v, ctx->n*ctx->dof, PETSC_DECIDE);
207: VecSetBlockSize(*v, ctx->dof);
208: VecSetType(*v,VECSTANDARD);
209: return(0);
210: }
214: PetscErrorCode DMInterpolationRestoreVector(DMInterpolationInfo ctx, Vec *v)
215: {
220: if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
221: VecDestroy(v);
222: return(0);
223: }
227: PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Triangle_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
228: {
229: PetscReal *v0, *J, *invJ, detJ;
230: PetscScalar *a, *coords;
231: PetscInt p;
235: PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);
236: VecGetArray(ctx->coords, &coords);
237: VecGetArray(v, &a);
238: for (p = 0; p < ctx->n; ++p) {
239: PetscInt c = ctx->cells[p];
240: PetscScalar *x = NULL;
241: PetscReal xi[4];
242: PetscInt d, f, comp;
244: DMPlexComputeCellGeometry(dm, c, v0, J, invJ, &detJ);
245: if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
246: DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);
247: for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];
249: for (d = 0; d < ctx->dim; ++d) {
250: xi[d] = 0.0;
251: for (f = 0; f < ctx->dim; ++f) xi[d] += invJ[d*ctx->dim+f]*0.5*PetscRealPart(coords[p*ctx->dim+f] - v0[f]);
252: for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] += PetscRealPart(x[(d+1)*ctx->dof+comp] - x[0*ctx->dof+comp])*xi[d];
253: }
254: DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);
255: }
256: VecRestoreArray(v, &a);
257: VecRestoreArray(ctx->coords, &coords);
258: PetscFree3(v0, J, invJ);
259: return(0);
260: }
264: PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Tetrahedron_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
265: {
266: PetscReal *v0, *J, *invJ, detJ;
267: PetscScalar *a, *coords;
268: PetscInt p;
272: PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);
273: VecGetArray(ctx->coords, &coords);
274: VecGetArray(v, &a);
275: for (p = 0; p < ctx->n; ++p) {
276: PetscInt c = ctx->cells[p];
277: const PetscInt order[3] = {2, 1, 3};
278: PetscScalar *x = NULL;
279: PetscReal xi[4];
280: PetscInt d, f, comp;
282: DMPlexComputeCellGeometry(dm, c, v0, J, invJ, &detJ);
283: if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
284: DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);
285: for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];
287: for (d = 0; d < ctx->dim; ++d) {
288: xi[d] = 0.0;
289: for (f = 0; f < ctx->dim; ++f) xi[d] += invJ[d*ctx->dim+f]*0.5*PetscRealPart(coords[p*ctx->dim+f] - v0[f]);
290: for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] += PetscRealPart(x[order[d]*ctx->dof+comp] - x[0*ctx->dof+comp])*xi[d];
291: }
292: DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);
293: }
294: VecRestoreArray(v, &a);
295: VecRestoreArray(ctx->coords, &coords);
296: PetscFree3(v0, J, invJ);
297: return(0);
298: }
302: PETSC_STATIC_INLINE PetscErrorCode QuadMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
303: {
304: const PetscScalar *vertices = (const PetscScalar*) ctx;
305: const PetscScalar x0 = vertices[0];
306: const PetscScalar y0 = vertices[1];
307: const PetscScalar x1 = vertices[2];
308: const PetscScalar y1 = vertices[3];
309: const PetscScalar x2 = vertices[4];
310: const PetscScalar y2 = vertices[5];
311: const PetscScalar x3 = vertices[6];
312: const PetscScalar y3 = vertices[7];
313: const PetscScalar f_1 = x1 - x0;
314: const PetscScalar g_1 = y1 - y0;
315: const PetscScalar f_3 = x3 - x0;
316: const PetscScalar g_3 = y3 - y0;
317: const PetscScalar f_01 = x2 - x1 - x3 + x0;
318: const PetscScalar g_01 = y2 - y1 - y3 + y0;
319: PetscScalar *ref, *real;
320: PetscErrorCode ierr;
323: VecGetArray(Xref, &ref);
324: VecGetArray(Xreal, &real);
325: {
326: const PetscScalar p0 = ref[0];
327: const PetscScalar p1 = ref[1];
329: real[0] = x0 + f_1 * p0 + f_3 * p1 + f_01 * p0 * p1;
330: real[1] = y0 + g_1 * p0 + g_3 * p1 + g_01 * p0 * p1;
331: }
332: PetscLogFlops(28);
333: VecRestoreArray(Xref, &ref);
334: VecRestoreArray(Xreal, &real);
335: return(0);
336: }
338: #include <petsc-private/dmimpl.h>
341: PETSC_STATIC_INLINE PetscErrorCode QuadJacobian_Private(SNES snes, Vec Xref, Mat *J, Mat *M, MatStructure *flag, void *ctx)
342: {
343: const PetscScalar *vertices = (const PetscScalar*) ctx;
344: const PetscScalar x0 = vertices[0];
345: const PetscScalar y0 = vertices[1];
346: const PetscScalar x1 = vertices[2];
347: const PetscScalar y1 = vertices[3];
348: const PetscScalar x2 = vertices[4];
349: const PetscScalar y2 = vertices[5];
350: const PetscScalar x3 = vertices[6];
351: const PetscScalar y3 = vertices[7];
352: const PetscScalar f_01 = x2 - x1 - x3 + x0;
353: const PetscScalar g_01 = y2 - y1 - y3 + y0;
354: PetscScalar *ref;
355: PetscErrorCode ierr;
358: VecGetArray(Xref, &ref);
359: {
360: const PetscScalar x = ref[0];
361: const PetscScalar y = ref[1];
362: const PetscInt rows[2] = {0, 1};
363: PetscScalar values[4];
365: values[0] = (x1 - x0 + f_01*y) * 0.5; values[1] = (x3 - x0 + f_01*x) * 0.5;
366: values[2] = (y1 - y0 + g_01*y) * 0.5; values[3] = (y3 - y0 + g_01*x) * 0.5;
367: MatSetValues(*J, 2, rows, 2, rows, values, INSERT_VALUES);
368: }
369: PetscLogFlops(30);
370: VecRestoreArray(Xref, &ref);
371: MatAssemblyBegin(*J, MAT_FINAL_ASSEMBLY);
372: MatAssemblyEnd(*J, MAT_FINAL_ASSEMBLY);
373: return(0);
374: }
378: PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Quad_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
379: {
380: DM dmCoord;
381: SNES snes;
382: KSP ksp;
383: PC pc;
384: Vec coordsLocal, r, ref, real;
385: Mat J;
386: PetscScalar *a, *coords;
387: PetscInt p;
391: DMGetCoordinatesLocal(dm, &coordsLocal);
392: DMGetCoordinateDM(dm, &dmCoord);
393: SNESCreate(PETSC_COMM_SELF, &snes);
394: SNESSetOptionsPrefix(snes, "quad_interp_");
395: VecCreate(PETSC_COMM_SELF, &r);
396: VecSetSizes(r, 2, 2);
397: VecSetType(r,dm->vectype);
398: VecDuplicate(r, &ref);
399: VecDuplicate(r, &real);
400: MatCreate(PETSC_COMM_SELF, &J);
401: MatSetSizes(J, 2, 2, 2, 2);
402: MatSetType(J, MATSEQDENSE);
403: MatSetUp(J);
404: SNESSetFunction(snes, r, QuadMap_Private, NULL);
405: SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);
406: SNESGetKSP(snes, &ksp);
407: KSPGetPC(ksp, &pc);
408: PCSetType(pc, PCLU);
409: SNESSetFromOptions(snes);
411: VecGetArray(ctx->coords, &coords);
412: VecGetArray(v, &a);
413: for (p = 0; p < ctx->n; ++p) {
414: PetscScalar *x = NULL, *vertices = NULL;
415: PetscScalar *xi;
416: PetscReal xir[2];
417: PetscInt c = ctx->cells[p], comp, coordSize, xSize;
419: /* Can make this do all points at once */
420: DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);
421: if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 4*2);
422: DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);
423: if (4*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 4*ctx->dof);
424: SNESSetFunction(snes, NULL, NULL, (void*) vertices);
425: SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);
426: VecGetArray(real, &xi);
427: xi[0] = coords[p*ctx->dim+0];
428: xi[1] = coords[p*ctx->dim+1];
429: VecRestoreArray(real, &xi);
430: SNESSolve(snes, real, ref);
431: VecGetArray(ref, &xi);
432: xir[0] = PetscRealPart(xi[0]);
433: xir[1] = PetscRealPart(xi[1]);
434: for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]*(1 - xir[0])*(1 - xir[1]) + x[1*ctx->dof+comp]*xir[0]*(1 - xir[1]) + x[2*ctx->dof+comp]*xir[0]*xir[1] + x[3*ctx->dof+comp]*(1 - xir[0])*xir[1];
436: VecRestoreArray(ref, &xi);
437: DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);
438: DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);
439: }
440: VecRestoreArray(v, &a);
441: VecRestoreArray(ctx->coords, &coords);
443: SNESDestroy(&snes);
444: VecDestroy(&r);
445: VecDestroy(&ref);
446: VecDestroy(&real);
447: MatDestroy(&J);
448: return(0);
449: }
453: PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
454: {
455: const PetscScalar *vertices = (const PetscScalar*) ctx;
456: const PetscScalar x0 = vertices[0];
457: const PetscScalar y0 = vertices[1];
458: const PetscScalar z0 = vertices[2];
459: const PetscScalar x1 = vertices[9];
460: const PetscScalar y1 = vertices[10];
461: const PetscScalar z1 = vertices[11];
462: const PetscScalar x2 = vertices[6];
463: const PetscScalar y2 = vertices[7];
464: const PetscScalar z2 = vertices[8];
465: const PetscScalar x3 = vertices[3];
466: const PetscScalar y3 = vertices[4];
467: const PetscScalar z3 = vertices[5];
468: const PetscScalar x4 = vertices[12];
469: const PetscScalar y4 = vertices[13];
470: const PetscScalar z4 = vertices[14];
471: const PetscScalar x5 = vertices[15];
472: const PetscScalar y5 = vertices[16];
473: const PetscScalar z5 = vertices[17];
474: const PetscScalar x6 = vertices[18];
475: const PetscScalar y6 = vertices[19];
476: const PetscScalar z6 = vertices[20];
477: const PetscScalar x7 = vertices[21];
478: const PetscScalar y7 = vertices[22];
479: const PetscScalar z7 = vertices[23];
480: const PetscScalar f_1 = x1 - x0;
481: const PetscScalar g_1 = y1 - y0;
482: const PetscScalar h_1 = z1 - z0;
483: const PetscScalar f_3 = x3 - x0;
484: const PetscScalar g_3 = y3 - y0;
485: const PetscScalar h_3 = z3 - z0;
486: const PetscScalar f_4 = x4 - x0;
487: const PetscScalar g_4 = y4 - y0;
488: const PetscScalar h_4 = z4 - z0;
489: const PetscScalar f_01 = x2 - x1 - x3 + x0;
490: const PetscScalar g_01 = y2 - y1 - y3 + y0;
491: const PetscScalar h_01 = z2 - z1 - z3 + z0;
492: const PetscScalar f_12 = x7 - x3 - x4 + x0;
493: const PetscScalar g_12 = y7 - y3 - y4 + y0;
494: const PetscScalar h_12 = z7 - z3 - z4 + z0;
495: const PetscScalar f_02 = x5 - x1 - x4 + x0;
496: const PetscScalar g_02 = y5 - y1 - y4 + y0;
497: const PetscScalar h_02 = z5 - z1 - z4 + z0;
498: const PetscScalar f_012 = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
499: const PetscScalar g_012 = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
500: const PetscScalar h_012 = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
501: PetscScalar *ref, *real;
502: PetscErrorCode ierr;
505: VecGetArray(Xref, &ref);
506: VecGetArray(Xreal, &real);
507: {
508: const PetscScalar p0 = ref[0];
509: const PetscScalar p1 = ref[1];
510: const PetscScalar p2 = ref[2];
512: real[0] = x0 + f_1*p0 + f_3*p1 + f_4*p2 + f_01*p0*p1 + f_12*p1*p2 + f_02*p0*p2 + f_012*p0*p1*p2;
513: real[1] = y0 + g_1*p0 + g_3*p1 + g_4*p2 + g_01*p0*p1 + g_01*p0*p1 + g_12*p1*p2 + g_02*p0*p2 + g_012*p0*p1*p2;
514: real[2] = z0 + h_1*p0 + h_3*p1 + h_4*p2 + h_01*p0*p1 + h_01*p0*p1 + h_12*p1*p2 + h_02*p0*p2 + h_012*p0*p1*p2;
515: }
516: PetscLogFlops(114);
517: VecRestoreArray(Xref, &ref);
518: VecRestoreArray(Xreal, &real);
519: return(0);
520: }
524: PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat *J, Mat *M, MatStructure *flag, void *ctx)
525: {
526: const PetscScalar *vertices = (const PetscScalar*) ctx;
527: const PetscScalar x0 = vertices[0];
528: const PetscScalar y0 = vertices[1];
529: const PetscScalar z0 = vertices[2];
530: const PetscScalar x1 = vertices[9];
531: const PetscScalar y1 = vertices[10];
532: const PetscScalar z1 = vertices[11];
533: const PetscScalar x2 = vertices[6];
534: const PetscScalar y2 = vertices[7];
535: const PetscScalar z2 = vertices[8];
536: const PetscScalar x3 = vertices[3];
537: const PetscScalar y3 = vertices[4];
538: const PetscScalar z3 = vertices[5];
539: const PetscScalar x4 = vertices[12];
540: const PetscScalar y4 = vertices[13];
541: const PetscScalar z4 = vertices[14];
542: const PetscScalar x5 = vertices[15];
543: const PetscScalar y5 = vertices[16];
544: const PetscScalar z5 = vertices[17];
545: const PetscScalar x6 = vertices[18];
546: const PetscScalar y6 = vertices[19];
547: const PetscScalar z6 = vertices[20];
548: const PetscScalar x7 = vertices[21];
549: const PetscScalar y7 = vertices[22];
550: const PetscScalar z7 = vertices[23];
551: const PetscScalar f_xy = x2 - x1 - x3 + x0;
552: const PetscScalar g_xy = y2 - y1 - y3 + y0;
553: const PetscScalar h_xy = z2 - z1 - z3 + z0;
554: const PetscScalar f_yz = x7 - x3 - x4 + x0;
555: const PetscScalar g_yz = y7 - y3 - y4 + y0;
556: const PetscScalar h_yz = z7 - z3 - z4 + z0;
557: const PetscScalar f_xz = x5 - x1 - x4 + x0;
558: const PetscScalar g_xz = y5 - y1 - y4 + y0;
559: const PetscScalar h_xz = z5 - z1 - z4 + z0;
560: const PetscScalar f_xyz = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
561: const PetscScalar g_xyz = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
562: const PetscScalar h_xyz = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
563: PetscScalar *ref;
564: PetscErrorCode ierr;
567: VecGetArray(Xref, &ref);
568: {
569: const PetscScalar x = ref[0];
570: const PetscScalar y = ref[1];
571: const PetscScalar z = ref[2];
572: const PetscInt rows[3] = {0, 1, 2};
573: PetscScalar values[9];
575: values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0;
576: values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0;
577: values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0;
578: values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0;
579: values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0;
580: values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0;
581: values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0;
582: values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0;
583: values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0;
585: MatSetValues(*J, 3, rows, 3, rows, values, INSERT_VALUES);
586: }
587: PetscLogFlops(152);
588: VecRestoreArray(Xref, &ref);
589: MatAssemblyBegin(*J, MAT_FINAL_ASSEMBLY);
590: MatAssemblyEnd(*J, MAT_FINAL_ASSEMBLY);
591: return(0);
592: }
596: PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
597: {
598: DM dmCoord;
599: SNES snes;
600: KSP ksp;
601: PC pc;
602: Vec coordsLocal, r, ref, real;
603: Mat J;
604: PetscScalar *a, *coords;
605: PetscInt p;
609: DMGetCoordinatesLocal(dm, &coordsLocal);
610: DMGetCoordinateDM(dm, &dmCoord);
611: SNESCreate(PETSC_COMM_SELF, &snes);
612: SNESSetOptionsPrefix(snes, "hex_interp_");
613: VecCreate(PETSC_COMM_SELF, &r);
614: VecSetSizes(r, 3, 3);
615: VecSetType(r,dm->vectype);
616: VecDuplicate(r, &ref);
617: VecDuplicate(r, &real);
618: MatCreate(PETSC_COMM_SELF, &J);
619: MatSetSizes(J, 3, 3, 3, 3);
620: MatSetType(J, MATSEQDENSE);
621: MatSetUp(J);
622: SNESSetFunction(snes, r, HexMap_Private, NULL);
623: SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);
624: SNESGetKSP(snes, &ksp);
625: KSPGetPC(ksp, &pc);
626: PCSetType(pc, PCLU);
627: SNESSetFromOptions(snes);
629: VecGetArray(ctx->coords, &coords);
630: VecGetArray(v, &a);
631: for (p = 0; p < ctx->n; ++p) {
632: PetscScalar *x = NULL, *vertices = NULL;
633: PetscScalar *xi;
634: PetscReal xir[3];
635: PetscInt c = ctx->cells[p], comp, coordSize, xSize;
637: /* Can make this do all points at once */
638: DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);
639: if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 8*3);
640: DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);
641: if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 8*ctx->dof);
642: SNESSetFunction(snes, NULL, NULL, (void*) vertices);
643: SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);
644: VecGetArray(real, &xi);
645: xi[0] = coords[p*ctx->dim+0];
646: xi[1] = coords[p*ctx->dim+1];
647: xi[2] = coords[p*ctx->dim+2];
648: VecRestoreArray(real, &xi);
649: SNESSolve(snes, real, ref);
650: VecGetArray(ref, &xi);
651: xir[0] = PetscRealPart(xi[0]);
652: xir[1] = PetscRealPart(xi[1]);
653: xir[2] = PetscRealPart(xi[2]);
654: for (comp = 0; comp < ctx->dof; ++comp) {
655: a[p*ctx->dof+comp] =
656: x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) +
657: x[3*ctx->dof+comp]* xir[0]*(1-xir[1])*(1-xir[2]) +
658: x[2*ctx->dof+comp]* xir[0]* xir[1]*(1-xir[2]) +
659: x[1*ctx->dof+comp]*(1-xir[0])* xir[1]*(1-xir[2]) +
660: x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])* xir[2] +
661: x[5*ctx->dof+comp]* xir[0]*(1-xir[1])* xir[2] +
662: x[6*ctx->dof+comp]* xir[0]* xir[1]* xir[2] +
663: x[7*ctx->dof+comp]*(1-xir[0])* xir[1]* xir[2];
664: }
665: VecRestoreArray(ref, &xi);
666: DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);
667: DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);
668: }
669: VecRestoreArray(v, &a);
670: VecRestoreArray(ctx->coords, &coords);
672: SNESDestroy(&snes);
673: VecDestroy(&r);
674: VecDestroy(&ref);
675: VecDestroy(&real);
676: MatDestroy(&J);
677: return(0);
678: }
682: /*
683: Input Parameters:
684: + ctx - The DMInterpolationInfo context
685: . dm - The DM
686: - x - The local vector containing the field to be interpolated
688: Output Parameters:
689: . v - The vector containing the interpolated values
690: */
691: PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v)
692: {
693: PetscInt dim, coneSize, n;
700: VecGetLocalSize(v, &n);
701: if (n != ctx->n*ctx->dof) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid input vector size %d should be %d", n, ctx->n*ctx->dof);
702: if (n) {
703: DMPlexGetDimension(dm, &dim);
704: DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);
705: if (dim == 2) {
706: if (coneSize == 3) {
707: DMInterpolate_Triangle_Private(ctx, dm, x, v);
708: } else if (coneSize == 4) {
709: DMInterpolate_Quad_Private(ctx, dm, x, v);
710: } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim);
711: } else if (dim == 3) {
712: if (coneSize == 4) {
713: DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);
714: } else {
715: DMInterpolate_Hex_Private(ctx, dm, x, v);
716: }
717: } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim);
718: }
719: return(0);
720: }
724: PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx)
725: {
730: VecDestroy(&(*ctx)->coords);
731: PetscFree((*ctx)->points);
732: PetscFree((*ctx)->cells);
733: PetscFree(*ctx);
734: *ctx = NULL;
735: return(0);
736: }