Actual source code: inpututils.c
1: /* $Id: inpututils.c,v 1.19 2001/08/07 21:31:52 bsmith Exp $ */
3: /*
4: Utilities for inputing, creating and managing simple two dimensional grids
5: */
7: #include src/dm/ao/aoimpl.h
8: #include petscbt.h
9: #include petscdraw.h
11: /*
12: cell_n - number of cells
13: max_cell - maximum space allocated for cell
14: cell_vertex - vertices of each cell
15: cell_edge - edges of the cell
16: cell_cell - neighbors of cell
17: vertex_n - number of vertices
18: vertex_max - maximum space allocated for vertices
19: x,y - vertex coordinates
21: xmin,ymin,xmax,ymax - bounding box of grid
23: edge_n - total edges in the grid
24: edge_vertex - vertex of all edges
25: edge_max - maximum space allocated for edge
26: edge_cell - two neighbor cells who share edge
28: vertex_boundary - indicates for each vertex if it is a boundary
30: */
36: int AOData2dGridToAOData(AOData2dGrid agrid,AOData *ao)
37: {
38: int ierr;
39: int *keys,nmax,i;
40: AOData aodata;
43: /*
44: Create the database
45: */
46: nmax = PetscMax(agrid->cell_n,agrid->vertex_n);
47: nmax = PetscMax(nmax,agrid->edge_n);
48: PetscMalloc(nmax*sizeof(int),&keys);
49: for (i=0; i<nmax; i++) {
50: keys[i] = i;
51: }
52: AODataCreateBasic(PETSC_COMM_WORLD,&aodata);
53: AODataKeyAdd(aodata,"cell",PETSC_DECIDE,agrid->cell_n);
54: AODataSegmentAdd(aodata,"cell","cell",4,agrid->cell_n,keys,agrid->cell_cell,PETSC_INT);
55: AODataSegmentAdd(aodata,"cell","vertex",4,agrid->cell_n,keys,agrid->cell_vertex,PETSC_INT);
56: AODataSegmentAdd(aodata,"cell","edge",4,agrid->cell_n,keys,agrid->cell_edge,PETSC_INT);
57: AODataKeyAdd(aodata,"edge",PETSC_DECIDE,agrid->edge_n);
58: AODataSegmentAdd(aodata,"edge","vertex",2,agrid->edge_n,keys,agrid->edge_vertex,PETSC_INT);
59: AODataSegmentAdd(aodata,"edge","cell",2,agrid->edge_n,keys,agrid->edge_cell,PETSC_INT);
60: AODataKeyAdd(aodata,"vertex",PETSC_DECIDE,agrid->vertex_n);
61: AODataSegmentAdd(aodata,"vertex","values",2,agrid->vertex_n,keys,agrid->vertex,PETSC_DOUBLE);
62: AODataSegmentAdd(aodata,"vertex","boundary",1,agrid->vertex_n,keys,agrid->vertex_boundary,PETSC_LOGICAL);
63: PetscFree(keys);
64: *ao = aodata;
65: return(0);
66: }
70: /*
71: User input the cell by drawing them one at a time
72: */
73: int AOData2dGridInput(AOData2dGrid agrid,PetscDraw draw)
74: {
75: PetscDraw popup; /* help window */
76: PetscDrawButton button; /* mouse button pressed */
77: int cn, ierr,*cell;
78: PetscReal *vertex,cx,cy;
79: char title[120];
82: agrid->cell_max = 500;
83: agrid->cell_n = 0;
84: agrid->vertex_max = 500;
85: agrid->vertex_n = 0;
86: agrid->xmin = PETSC_MAX;
87: agrid->xmax = PETSC_MIN;
88: agrid->ymin = PETSC_MAX;
89: agrid->ymax = PETSC_MIN;
91: /*
92: Allocate large arrays to hold the nodes and cellrilateral lists
93: */
94: PetscMalloc(2*agrid->vertex_max*sizeof(PetscReal),&agrid->vertex);
95: vertex = agrid->vertex;
96: PetscMalloc(4*agrid->cell_max*sizeof(int),&agrid->cell_vertex);
97: cell = agrid->cell_vertex;
100: /*
101: Open help window and enter helpful messages
102: */
103: PetscDrawGetPopup(draw,&popup);
104: PetscDrawString(popup,.1,.9,PETSC_DRAW_BLUE,"Use left button to\n enter cell.");
105: PetscDrawString(popup,.1,.7,PETSC_DRAW_BLUE,"Use center button to\n end.");
106: PetscDrawFlush(popup);
108: PetscDrawGetMouseButton(draw,&button,&cx,&cy,PETSC_NULL,PETSC_NULL);
109: AOData2dGridAddNode(agrid,cx,cy,&cn);
110: cell[0] = cn;
111: sprintf(title,"Input grid: Number vertex %d Number cell %d",agrid->vertex_n,agrid->cell_n);
112: PetscDrawSetTitle(draw,title);
113: while (button == BUTTON_LEFT) {
114: /* wait for second vertex */
115: PetscDrawGetMouseButton(draw,&button,&cx,&cy,PETSC_NULL,PETSC_NULL);
116: if (button != BUTTON_LEFT) {
117: SETERRQ(1,"Must press left button to complete cellrilateral");
118: }
119: AOData2dGridAddNode(agrid,cx,cy,&cn);
120: cell[4*agrid->cell_n+1] = cn;
121: PetscDrawLine(draw,vertex[2*cell[4*agrid->cell_n]],vertex[1+2*cell[4*agrid->cell_n]],
122: vertex[2*cell[4*agrid->cell_n+1]],vertex[1+2*cell[4*agrid->cell_n+1]],
123: PETSC_DRAW_RED);
124: sprintf(title,"Input grid: Number vertex %d Number cell %d",agrid->vertex_n,agrid->cell_n);
125: PetscDrawSetTitle(draw,title);
126: /* wait for third vertex */
127: PetscDrawGetMouseButton(draw,&button,&cx,&cy,PETSC_NULL,PETSC_NULL);
128: if (button != BUTTON_LEFT) {
129: SETERRQ(1,"Must press left button to complete cellrilateral");
130: }
131: AOData2dGridAddNode(agrid,cx,cy,&cn);
132: cell[4*agrid->cell_n+2] = cn;
133: PetscDrawLine(draw,vertex[2*cell[4*agrid->cell_n+1]],vertex[1+2*cell[4*agrid->cell_n+1]],
134: vertex[2*cell[4*agrid->cell_n+2]],vertex[1+2*cell[4*agrid->cell_n+2]],
135: PETSC_DRAW_RED);
136: sprintf(title,"Input grid: Number vertex %d Number cell %d",agrid->vertex_n,agrid->cell_n);
137: PetscDrawSetTitle(draw,title);
138: /* wait for fourth vertex */
139: PetscDrawGetMouseButton(draw,&button,&cx,&cy,PETSC_NULL,PETSC_NULL);
140: if (button != BUTTON_LEFT) {
141: SETERRQ(1,"Must press left button to complete cellrilateral");
142: }
143: AOData2dGridAddNode(agrid,cx,cy,&cn);
144: cell[4*agrid->cell_n+3] = cn;
145: PetscDrawLine(draw,vertex[2*cell[4*agrid->cell_n+2]],vertex[1+2*cell[4*agrid->cell_n+2]],
146: vertex[2*cell[4*agrid->cell_n+3]],vertex[1+2*cell[4*agrid->cell_n+3]],
147: PETSC_DRAW_RED);
148: PetscDrawLine(draw,vertex[2*cell[4*agrid->cell_n]],vertex[1+2*cell[4*agrid->cell_n]],
149: vertex[2*cell[4*agrid->cell_n+3]],vertex[1+2*cell[4*agrid->cell_n+3]],
150: PETSC_DRAW_RED);
151: agrid->cell_n++;
152: sprintf(title,"Input grid: Number vertex %d Number cell %d",agrid->vertex_n,agrid->cell_n);
153: PetscDrawSetTitle(draw,title);
155: /* Get the first for the next cellralateral, or BUTTON_CENTER to end */
156: PetscDrawGetMouseButton(draw,&button,&cx,&cy,PETSC_NULL,PETSC_NULL);
157: if (button != BUTTON_LEFT) {break;}
158: AOData2dGridAddNode(agrid,cx,cy,&cn);
159: cell[4*agrid->cell_n] = cn;
161: sprintf(title,"Input grid: Number vertex %d Number cell %d",agrid->vertex_n,agrid->cell_n);
162: PetscDrawSetTitle(draw,title);
163: }
164: return(0);
165: }
169: /*
170: Changes the node numbering for the cell to make sure they are all in
171: clockwise ordering
172: */
173: int AOData2dGridFlipCells(AOData2dGrid agrid)
174: {
175: int i,*cell = agrid->cell_vertex, cell_n = agrid->cell_n;
176: PetscReal *vertex = agrid->vertex, sign;
179: for (i=0; i<cell_n; i++) {
180: /*
181: compute the quantity
183: x0 x1 x2 x3
184: y0 y1 y2 y3
185: */
187: sign = vertex[2*cell[4*i]]*vertex[1+2*cell[4*i+1]] + vertex[2*cell[4*i+1]]*vertex[1+2*cell[4*i+2]] +
188: vertex[2*cell[4*i+2]]*vertex[1+2*cell[4*i+3]] + vertex[2*cell[4*i+3]]*vertex[1+2*cell[4*i]] -
189: vertex[1+2*cell[4*i]]*vertex[2*cell[4*i+1]] - vertex[1+2*cell[4*i+1]]*vertex[2*cell[4*i+2]] -
190: vertex[1+2*cell[4*i+2]]*vertex[2*cell[4*i+3]] - vertex[1+2*cell[4*i+3]]*vertex[2*cell[4*i]];
192: if (sign == 0.0) {
193: SETERRQ(1,"Bad cell");
194: } else if (sign > 0) {
195: int q1tmp = cell[4*i+1];
196: cell[4*i+1] = cell[4*i+3];
197: cell[4*i+3] = q1tmp;
198: }
199: }
200: return(0);
201: }
205: /*
206: AOData2dGridAddNode - Maintains a list of nodes given so far
207: */
208: int AOData2dGridAddNode(AOData2dGrid agrid, PetscReal cx, PetscReal cy, int *cn)
209: {
210: int i;
213: for (i=0; i<agrid->vertex_n; i++) {
214: if ((PetscAbsReal(agrid->vertex[2*i] - cx) < 1.e-9) && (PetscAbsReal(agrid->vertex[1+2*i] - cy) < 1.e-9)) {
215: *cn = i;
216: return(0);
217: }
218: }
219: agrid->vertex[2*agrid->vertex_n] = cx;
220: agrid->vertex[1+2*agrid->vertex_n] = cy;
221: *cn = (agrid->vertex_n)++;
223: if (cx < agrid->xmin) agrid->xmin = cx;
224: else if (cx > agrid->xmax) agrid->xmax = cx;
225: if (cy < agrid->ymin) agrid->ymin = cy;
226: else if (cy > agrid->ymax) agrid->ymax = cy;
227: return(0);
228: }
232: int AOData2dGridComputeNeighbors(AOData2dGrid agrid)
233: {
234: int i,j,*cell_edge,*edge_cell,*edge,*cell,*neighbors,e,ierr;
237: agrid->edge_max = 2*agrid->vertex_n;
238: agrid->edge_n = 0;
239: PetscMalloc(2*agrid->edge_max*sizeof(int),&agrid->edge_vertex);
240: edge = agrid->edge_vertex;
241: PetscMalloc(4*agrid->cell_max*sizeof(int),agrid->cell_edge);
242: cell_edge = agrid->cell_edge;
243: PetscMalloc(2*agrid->edge_max*sizeof(int),&agrid->edge_cell);
244: edge_cell = agrid->edge_cell;
246: cell = agrid->cell_vertex;
248: /*
249: Mark all neighbors (to start) with -1 to indicate missing neighbor
250: */
251: for (i=0; i<2*agrid->edge_max; i++) {
252: edge_cell[i] = -1;
253: }
255: for (i=0; i<agrid->cell_n; i++) {
256: for (j=0; j<agrid->edge_n; j++) {
257: if (cell[4*i] == edge[2*j+1] && cell[4*i+1] == edge[2*j]) {
258: cell_edge[4*i] = j;
259: edge_cell[2*j+1] = i;
260: goto found0;
261: }
262: }
263: /*
264: Add a new edge to the list
265: */
266: edge_cell[2*agrid->edge_n] = i;
267: edge[2*agrid->edge_n] = cell[4*i];
268: edge[2*agrid->edge_n+1] = cell[4*i+1];
269: cell_edge[4*i] = agrid->edge_n;
270: agrid->edge_n++;
271: found0:;
272: for (j=0; j<agrid->edge_n; j++) {
273: if (cell[4*i+1] == edge[2*j+1] && cell[4*i+2] == edge[2*j]) {
274: cell_edge[4*i+1] = j;
275: edge_cell[2*j+1] = i;
276: goto found1;
277: }
278: }
279: /*
280: Add a new edge to the list
281: */
282: edge_cell[2*agrid->edge_n] = i;
283: edge[2*agrid->edge_n] = cell[4*i+1];
284: edge[2*agrid->edge_n+1] = cell[4*i+2];
285: cell_edge[4*i+1] = agrid->edge_n;
286: agrid->edge_n++;
287: found1:;
288: for (j=0; j<agrid->edge_n; j++) {
289: if (cell[4*i+2] == edge[2*j+1] && cell[4*i+3] == edge[2*j]) {
290: cell_edge[4*i+2] = j;
291: edge_cell[2*j+1] = i;
292: goto found2;
293: }
294: }
295: /*
296: Add a new edge to the list
297: */
298: edge_cell[2*agrid->edge_n] = i;
299: edge[2*agrid->edge_n] = cell[4*i+2];
300: edge[2*agrid->edge_n+1] = cell[4*i+3];
301: cell_edge[4*i+2] = agrid->edge_n;
302: agrid->edge_n++;
303: found2:;
304: for (j=0; j<agrid->edge_n; j++) {
305: if (cell[4*i+3] == edge[2*j+1] && cell[4*i] == edge[2*j]) {
306: cell_edge[4*i+3] = j;
307: edge_cell[2*j+1] = i;
308: goto found3;
309: }
310: }
311: /*
312: Add a new edge to the list
313: */
314: edge_cell[2*agrid->edge_n] = i;
315: edge[2*agrid->edge_n] = cell[4*i+3];
316: edge[2*agrid->edge_n+1] = cell[4*i];
317: cell_edge[4*i+3] = agrid->edge_n;
318: agrid->edge_n++;
319: found3:;
321: }
323: PetscMalloc(4*agrid->cell_n*sizeof(int),&agrid->cell_cell);
324: neighbors = agrid->cell_cell;
325: for (i=0; i<agrid->cell_n; i++) {
326: for (j=0; j<4; j++) {
327: e = 2*agrid->cell_edge[4*i+j];
329: /* get the edge neighbor that is not the current cell */
330: if (i == agrid->edge_cell[e]) e++;
331: neighbors[4*i+j] = agrid->edge_cell[e];
332: }
333: }
335: return(0);
336: }
340: int AOData2dGridComputeVertexBoundary(AOData2dGrid agrid)
341: {
342: int i,j,*count,*cell_vertex = agrid->cell_vertex,ierr;
345: /*
346: allocate bitarray for boundary info
347: */
348: PetscBTCreate(agrid->vertex_n,agrid->vertex_boundary);
350: /*
351: count contains number of cell that contain the given vertex
352: */
353: PetscMalloc(agrid->vertex_n*sizeof(int),&count);
354: PetscMemzero(count,agrid->vertex_n*sizeof(int));
356: for (i=0; i<agrid->cell_n; i++) {
357: for (j=0; j<4; j++) {
358: count[cell_vertex[4*i+j]]++;
359: }
360: }
363: for (i=0; i<agrid->vertex_n; i++) {
364: /* UGLY! Just for a quick solution: I want Dirichlet b.c. only at left edge! */
365: PetscTruth neumann_bc;
366: PetscOptionsHasName(PETSC_NULL,"-dirichlet_on_left",&neumann_bc);
367: if (neumann_bc) {
368: if ((count[i] < 4) && (agrid->vertex[2*i] == agrid->xmin)) {
369: PetscBTSet(agrid->vertex_boundary,i);
370: }
371: } else {
372: if (count[i] < 4) {
373: PetscBTSet(agrid->vertex_boundary,i);
374: }
375: }
376: }
378: PetscFree(count);
380: return(0);
381: }
385: /*
386: Show the numbering of the vertex, cell and edge
387: */
388: int AOData2dGridDraw(AOData2dGrid agrid,PetscDraw draw)
389: {
390: int i, *cell = agrid->cell_vertex, *edge = agrid->edge_vertex,ierr;
391: char str[5];
392: PetscReal *vertex = agrid->vertex,xx,yy,xmin,xmax,ymin,ymax,h,w;
395: w = agrid->xmax - agrid->xmin;
396: h = agrid->ymax - agrid->ymin;
397: xmin = agrid->xmin - .1*w;
398: xmax = agrid->xmax + .1*w;
399: ymin = agrid->ymin - .1*h;
400: ymax = agrid->ymax + .1*h;
401: PetscDrawSetCoordinates(draw,xmin,ymin,xmax,ymax);
403: /*
404: Number the vertex
405: */
406: for (i=0; i<agrid->vertex_n; i++) {
407: sprintf(str,"%d",i);
408: PetscDrawString(draw,vertex[2*i],vertex[1+2*i],PETSC_DRAW_BLUE,str);
409: }
411: /*
412: Number the cell
413: */
414: for (i=0; i<agrid->cell_n; i++) {
415: sprintf(str,"%d",i);
416: xx = .25*(vertex[2*cell[4*i]] + vertex[2*cell[4*i+1]] + vertex[2*cell[4*i+2]] + vertex[2*cell[4*i+3]]);
417: yy = .25*(vertex[1+2*cell[4*i]] + vertex[1+2*cell[4*i+1]] + vertex[1+2*cell[4*i+2]] + vertex[1+2*cell[4*i+3]]);
418: PetscDrawString(draw,xx,yy,PETSC_DRAW_GREEN,str);
419: }
421: /*
422: Number the edge
423: */
424: for (i=0; i<agrid->edge_n; i++) {
425: sprintf(str,"%d",i);
426: xx = .5*(vertex[2*edge[2*i]] + vertex[2*edge[2*i+1]]);
427: yy = .5*(vertex[1+2*edge[2*i]] + vertex[1+2*edge[2*i+1]]);
428: PetscDrawLine(draw,vertex[2*edge[2*i]],vertex[1+2*edge[2*i]],vertex[2*edge[2*i+1]],vertex[1+2*edge[2*i+1]],PETSC_DRAW_BLACK);
429: PetscDrawString(draw,xx,yy,PETSC_DRAW_VIOLET,str);
430: }
432: return(0);
433: }
437: /*
438: Frees all the memory space allocated in AGrid
439: */
440: int AOData2dGridDestroy(AOData2dGrid agrid)
441: {
445: PetscFree(agrid->vertex);
446: PetscFree(agrid->cell_vertex);
447: PetscFree(agrid->cell_edge);
448: PetscFree(agrid->edge_vertex);
449: PetscFree(agrid->edge_cell);
450: PetscFree(agrid->cell_cell);
451: PetscFree(agrid->vertex_boundary);
452: PetscFree(agrid);
453: return(0);
454: }
458: /*
459:
460: */
461: int AOData2dGridCreate(AOData2dGrid *agrid)
462: {
465: PetscNew(struct _p_AOData2dGrid,agrid);
466: return(0);
467: }