Actual source code: ex50.c
1: static char help[] = "Test GLVis high-order support with DMDAs\n\n";
3: #include <petscdm.h>
4: #include <petscdmda.h>
5: #include <petscdmplex.h>
6: #include <petscdt.h>
8: static PetscErrorCode MapPoint(PetscScalar xyz[],PetscScalar mxyz[])
9: {
10: PetscScalar x,y,z,x2,y2,z2;
12: x = xyz[0];
13: y = xyz[1];
14: z = xyz[2];
15: x2 = x*x;
16: y2 = y*y;
17: z2 = z*z;
18: mxyz[0] = x*PetscSqrtScalar(1.0-y2/2.0-z2/2.0 + y2*z2/3.0);
19: mxyz[1] = y*PetscSqrtScalar(1.0-z2/2.0-x2/2.0 + z2*x2/3.0);
20: mxyz[2] = z*PetscSqrtScalar(1.0-x2/2.0-y2/2.0 + x2*y2/3.0);
21: return 0;
22: }
24: static PetscErrorCode test_3d(PetscInt cells[], PetscBool plex, PetscBool ho)
25: {
26: DM dm;
27: Vec v;
28: PetscScalar *c;
29: PetscInt nl,i;
30: PetscReal u[3] = {1.0,1.0,1.0}, l[3] = {-1.0,-1.0,-1.0};
34: if (ho) {
35: u[0] = 2.*cells[0];
36: u[1] = 2.*cells[1];
37: u[2] = 2.*cells[2];
38: l[0] = 0.;
39: l[1] = 0.;
40: l[2] = 0.;
41: }
42: if (plex) {
43: DMCreate(PETSC_COMM_WORLD, &dm);
44: DMSetType(dm, DMPLEX);
45: DMSetFromOptions(dm);
46: } else {
47: DMDACreate3d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,cells[0]+1,cells[1]+1,cells[2]+1,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,NULL,&dm);
48: }
49: DMSetUp(dm);
50: if (!plex) {
51: DMDASetUniformCoordinates(dm,l[0],u[0],l[1],u[1],l[2],u[2]);
52: }
53: if (ho) { /* each element mapped to a sphere */
54: DM cdm;
55: Vec cv;
56: PetscSection cSec;
57: DMDACoor3d ***_coords;
58: PetscScalar shift[3],*cptr;
59: PetscInt nel,dof = 3,nex,ney,nez,gx = 0,gy = 0,gz = 0;
60: PetscInt i,j,k,pi,pj,pk;
61: PetscReal *nodes,*weights;
62: char name[256];
64: PetscOptionsGetInt(NULL,NULL,"-order",&dof,NULL);
65: dof += 1;
67: PetscMalloc1(dof,&nodes);
68: PetscMalloc1(dof,&weights);
69: PetscDTGaussLobattoLegendreQuadrature(dof,PETSCGAUSSLOBATTOLEGENDRE_VIA_LINEAR_ALGEBRA,nodes,weights);
70: DMGetCoordinatesLocal(dm,&cv);
71: DMGetCoordinateDM(dm,&cdm);
72: if (plex) {
73: PetscInt cEnd,cStart;
75: DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);
76: DMGetCoordinateSection(dm,&cSec);
77: nel = cEnd - cStart;
78: nex = nel;
79: ney = 1;
80: nez = 1;
81: } else {
82: DMDAVecGetArray(cdm,cv,&_coords);
83: DMDAGetElementsCorners(dm,&gx,&gy,&gz);
84: DMDAGetElementsSizes(dm,&nex,&ney,&nez);
85: nel = nex*ney*nez;
86: }
87: VecCreate(PETSC_COMM_WORLD,&v);
88: VecSetSizes(v,3*dof*dof*dof*nel,PETSC_DECIDE);
89: VecSetType(v,VECSTANDARD);
90: VecGetArray(v,&c);
91: cptr = c;
92: for (k=gz;k<gz+nez;k++) {
93: for (j=gy;j<gy+ney;j++) {
94: for (i=gx;i<gx+nex;i++) {
95: if (plex) {
96: PetscScalar *t = NULL;
98: DMPlexVecGetClosure(dm,cSec,cv,i,NULL,&t);
99: shift[0] = t[0];
100: shift[1] = t[1];
101: shift[2] = t[2];
102: DMPlexVecRestoreClosure(dm,cSec,cv,i,NULL,&t);
103: } else {
104: shift[0] = _coords[k][j][i].x;
105: shift[1] = _coords[k][j][i].y;
106: shift[2] = _coords[k][j][i].z;
107: }
108: for (pk=0;pk<dof;pk++) {
109: PetscScalar xyz[3];
111: xyz[2] = nodes[pk];
112: for (pj=0;pj<dof;pj++) {
113: xyz[1] = nodes[pj];
114: for (pi=0;pi<dof;pi++) {
115: xyz[0] = nodes[pi];
116: MapPoint(xyz,cptr);
117: cptr[0] += shift[0];
118: cptr[1] += shift[1];
119: cptr[2] += shift[2];
120: cptr += 3;
121: }
122: }
123: }
124: }
125: }
126: }
127: if (!plex) {
128: DMDAVecRestoreArray(cdm,cv,&_coords);
129: }
130: VecRestoreArray(v,&c);
131: PetscSNPrintf(name,sizeof(name),"FiniteElementCollection: L2_T1_3D_P%D",dof-1);
132: PetscObjectSetName((PetscObject)v,name);
133: PetscObjectCompose((PetscObject)dm,"_glvis_mesh_coords",(PetscObject)v);
134: VecDestroy(&v);
135: PetscFree(nodes);
136: PetscFree(weights);
137: DMViewFromOptions(dm,NULL,"-view");
138: } else { /* map the whole domain to a sphere */
139: DMGetCoordinates(dm,&v);
140: VecGetLocalSize(v,&nl);
141: VecGetArray(v,&c);
142: for (i=0;i<nl/3;i++) {
143: MapPoint(c+3*i,c+3*i);
144: }
145: VecRestoreArray(v,&c);
146: DMSetCoordinates(dm,v);
147: DMViewFromOptions(dm,NULL,"-view");
148: }
149: DMDestroy(&dm);
150: return(0);
151: }
153: int main(int argc, char *argv[])
154: {
156: PetscBool ho = PETSC_TRUE;
157: PetscBool plex = PETSC_FALSE;
158: PetscInt cells[3] = {2,2,2};
160: PetscInitialize(&argc,&argv,0,help);if (ierr) return ierr;
161: PetscOptionsGetBool(NULL,NULL,"-ho",&ho,NULL);
162: PetscOptionsGetBool(NULL,NULL,"-plex",&plex,NULL);
163: PetscOptionsGetInt(NULL,NULL,"-nex",&cells[0],NULL);
164: PetscOptionsGetInt(NULL,NULL,"-ney",&cells[1],NULL);
165: PetscOptionsGetInt(NULL,NULL,"-nez",&cells[2],NULL);
166: test_3d(cells,plex,ho);
167: PetscFinalize();
168: return ierr;
169: }
171: /*TEST
173: testset:
174: nsize: 1
175: args: -view glvis:
177: test:
178: suffix: dmda_glvis_ho
179: args: -nex 1 -ney 1 -nez 1
181: test:
182: suffix: dmda_glvis_lo
183: args: -ho 0
185: test:
186: suffix: dmplex_glvis_ho
187: args: -nex 1 -ney 1 -nez 1
189: test:
190: suffix: dmplex_glvis_lo
191: args: -ho 0
193: TEST*/