download the original source code.
1 /* Save a structured n x n mesh of square elements on the unit square into a
2 GLVis mesh file with the given name. */
3 void GLVis_PrintGlobalSquareMesh(const char *meshfile, int n)
4 {
5 FILE *file;
6
7 int Dim = 2;
8 int NumOfVertices = (n+1)*(n+1);
9 int NumOfElements = n*n;
10
11 int i, j;
12 double x, y;
13 double h = 1.0/n;
14
15 if ((file = fopen(meshfile, "w")) == NULL)
16 {
17 printf("Error: can't open output file %s\n", meshfile);
18 exit(1);
19 }
20
21 /* mesh header */
22 fprintf(file, "MFEM mesh v1.0\n");
23 fprintf(file, "\ndimension\n");
24 fprintf(file, "%d\n", Dim);
25
26 /* mesh elements */
27 fprintf(file, "\nelements\n");
28 fprintf(file, "%d\n", NumOfElements);
29 for (j = 0; j < n; j++)
30 for (i = 0; i < n; i++)
31 fprintf(file, "1 3 %d %d %d %d\n", i + j*(n+1), i + 1 +j*(n+1),
32 i + 1 + (j+1)*(n+1), i + (j+1)*(n+1));
33
34 /* boundary will be generated by GLVis */
35 fprintf(file, "\nboundary\n");
36 fprintf(file, "0\n");
37
38 /* mesh vertices */
39 fprintf(file, "\nvertices\n");
40 fprintf(file, "%d\n", NumOfVertices);
41 fprintf(file, "%d\n", Dim);
42 for (j = 0; j < n+1; j++)
43 for (i = 0; i < n+1; i++)
44 {
45 x = i*h;
46 y = j*h;
47 fprintf(file, "%.14e %.14e\n", x, y);
48 }
49
50 fflush(file);
51 fclose(file);
52 }
53
54 /* Save a structured nx x ny mesh of square elements of size h, globally
55 translated by (x0,y0), into a GLVis mesh file with the given prefix. */
56 void GLVis_PrintLocalSquareMesh(const char *meshfile_prefix, int nx, int ny,
57 double h, double x0, double y0, int myid)
58 {
59 FILE *file;
60 char meshfile[255];
61
62 int Dim = 2;
63 int NumOfVertices = (nx+1)*(ny+1);
64 int NumOfElements = nx*ny;
65
66 int i, j;
67 double x, y;
68
69 sprintf(meshfile, "%s.%06d", meshfile_prefix, myid);
70 if ((file = fopen(meshfile, "w")) == NULL)
71 {
72 printf("Error: can't open output file %s\n", meshfile);
73 exit(1);
74 }
75
76 /* mesh header */
77 fprintf(file, "MFEM mesh v1.0\n");
78 fprintf(file, "\ndimension\n");
79 fprintf(file, "%d\n", Dim);
80
81 /* mesh elements */
82 fprintf(file, "\nelements\n");
83 fprintf(file, "%d\n", NumOfElements);
84 for (j = 0; j < ny; j++)
85 for (i = 0; i < nx; i++)
86 fprintf(file, "1 3 %d %d %d %d\n", i + j*(nx+1), i + 1 +j*(nx+1),
87 i + 1 + (j+1)*(nx+1), i + (j+1)*(nx+1));
88
89 /* boundary will be generated by GLVis */
90 fprintf(file, "\nboundary\n");
91 fprintf(file, "0\n");
92
93 /* mesh vertices */
94 fprintf(file, "\nvertices\n");
95 fprintf(file, "%d\n", NumOfVertices);
96 fprintf(file, "%d\n", Dim);
97 for (j = 0; j < ny+1; j++)
98 for (i = 0; i < nx+1; i++)
99 {
100 x = x0+i*h;
101 y = y0+j*h;
102 fprintf(file, "%.14e %.14e\n", x, y);
103 }
104
105 fflush(file);
106 fclose(file);
107 }
108
109 /* Save a structured n x n mesh of gamma-angled rhombuses, globally rotated by
110 angle gamma*myid, into a GLVis mesh file with the given prefix. */
111 void GLVis_PrintLocalRhombusMesh(const char *meshfile_prefix,
112 int n, int myid, double gamma)
113 {
114 FILE *file;
115 char meshfile[255];
116
117 int Dim = 2;
118 int NumOfVertices = (n+1)*(n+1);
119 int NumOfElements = n*n;
120
121 int i, j;
122 double x, y;
123 double h = 1.0/n;
124
125 double rho = gamma*myid;
126 double sg = sin(gamma);
127 double cg = cos(gamma);
128 double sr = sin(rho);
129 double cr = cos(rho);
130
131 sprintf(meshfile, "%s.%06d", meshfile_prefix, myid);
132 if ((file = fopen(meshfile, "w")) == NULL)
133 {
134 printf("Error: can't open output file %s\n", meshfile);
135 exit(1);
136 }
137
138 /* mesh header */
139 fprintf(file, "MFEM mesh v1.0\n");
140 fprintf(file, "\ndimension\n");
141 fprintf(file, "%d\n", Dim);
142
143 /* mesh elements */
144 fprintf(file, "\nelements\n");
145 fprintf(file, "%d\n", NumOfElements);
146 for (j = 0; j < n; j++)
147 for (i = 0; i < n; i++)
148 fprintf(file, "1 3 %d %d %d %d\n", i + j*(n+1), i + 1 +j*(n+1),
149 i + 1 + (j+1)*(n+1), i + (j+1)*(n+1));
150
151 /* boundary will be generated by GLVis */
152 fprintf(file, "\nboundary\n");
153 fprintf(file, "0\n");
154
155 /* mesh vertices */
156 fprintf(file, "\nvertices\n");
157 fprintf(file, "%d\n", NumOfVertices);
158 fprintf(file, "%d\n", Dim);
159 for (j = 0; j < n+1; j++)
160 for (i = 0; i < n+1; i++)
161 {
162 x = i*h + cg*j*h;
163 y = sg*j*h;
164 fprintf(file, "%.14e %.14e\n", cr*x - sr*y, sr*x + cr*y);
165 }
166
167 fflush(file);
168 fclose(file);
169 }
170
171 /* Save a structured nx x ny x nz mesh of cubic elements of size h, globally
172 translated by (x0,y0,z0), into a GLVis mesh file with the given prefix. */
173 void GLVis_PrintLocalCubicMesh(const char *meshfile_prefix,
174 int nx, int ny, int nz, double h,
175 double x0, double y0, double z0, int myid)
176 {
177 FILE *file;
178 char meshfile[255];
179
180 int Dim = 3;
181 int NumOfVertices = (nx+1)*(ny+1)*(nz+1);
182 int NumOfElements = nx*ny*nz;
183
184 int i, j, k;
185 double x, y, z;
186
187 sprintf(meshfile, "%s.%06d", meshfile_prefix, myid);
188 if ((file = fopen(meshfile, "w")) == NULL)
189 {
190 printf("Error: can't open output file %s\n", meshfile);
191 exit(1);
192 }
193
194 /* mesh header */
195 fprintf(file, "MFEM mesh v1.0\n");
196 fprintf(file, "\ndimension\n");
197 fprintf(file, "%d\n", Dim);
198
199 /* mesh elements */
200 fprintf(file, "\nelements\n");
201 fprintf(file, "%d\n", NumOfElements);
202 for (k = 0; k < nz; k++)
203 for (j = 0; j < ny; j++)
204 for (i = 0; i < nx; i++)
205 fprintf(file, "1 5 %d %d %d %d %d %d %d %d\n",
206 i + j*(nx+1) + k*(nx+1)*(ny+1),
207 i + 1 +j*(nx+1) + k*(nx+1)*(ny+1),
208 i + 1 + (j+1)*(nx+1) + k*(nx+1)*(ny+1),
209 i + (j+1)*(nx+1) + k*(nx+1)*(ny+1),
210 i + j*(nx+1) + (k+1)*(nx+1)*(ny+1),
211 i + 1 +j*(nx+1) + (k+1)*(nx+1)*(ny+1),
212 i + 1 + (j+1)*(nx+1) + (k+1)*(nx+1)*(ny+1),
213 i + (j+1)*(nx+1) + (k+1)*(nx+1)*(ny+1));
214
215 /* boundary will be generated by GLVis */
216 fprintf(file, "\nboundary\n");
217 fprintf(file, "0\n");
218
219 /* mesh vertices */
220 fprintf(file, "\nvertices\n");
221 fprintf(file, "%d\n", NumOfVertices);
222 fprintf(file, "%d\n", Dim);
223 for (k = 0; k < nz+1; k++)
224 for (j = 0; j < ny+1; j++)
225 for (i = 0; i < nx+1; i++)
226 {
227 x = x0+i*h;
228 y = y0+j*h;
229 z = z0+k*h;
230 fprintf(file, "%.14e %.14e %.14e\n", x, y, z);
231 }
232
233 fflush(file);
234 fclose(file);
235 }
236
237 #include "HYPRE_sstruct_mv.h"
238 #include "_hypre_sstruct_mv.h"
239
240 /* Save a GLVis mesh file with the given prefix corresponding to the input
241 SStruct grid assuming that the cells in each part are the same. The optional
242 trans and origin parameters specify the coordinate transformation for each
243 part, relative to a square Cartesian grid. */
244 void GLVis_PrintSStructGrid(HYPRE_SStructGrid grid,
245 const char *meshfile_prefix, int myid,
246 double *trans, double *origin)
247 {
248 FILE *file;
249 char meshfile[255];
250
251 int dim = ((hypre_SStructGrid *)grid)->ndim;
252 int cellNV = (dim == 2) ? 4 : 8;
253 int elemid = 2*dim-1;
254 int nvert, nelem;
255
256 hypre_StructGrid *part;
257 int p, nparts = ((hypre_SStructGrid *)grid)->nparts;
258 int given_trans = (trans != NULL && origin != NULL);
259 double *T = trans, *O = origin;
260
261 hypre_BoxArray *boxes;
262 hypre_Box *box;
263 int b, ncells;
264
265 nvert = nelem = 0;
266 for (p = 0; p < nparts; p++)
267 {
268 part = ((hypre_SStructGrid *)grid)->pgrids[p]->sgrids[0];
269 boxes = hypre_StructGridBoxes(part);
270 for (b = 0; b < hypre_BoxArraySize(boxes); b++)
271 {
272 box = hypre_BoxArrayBox(boxes, b);
273 ncells = hypre_BoxVolume(box);
274 nvert += ncells*cellNV;
275 nelem += ncells;
276 }
277 }
278
279 {
280 int i, j, k, v, vert;
281 double x0, y0, z0, h;
282
283 sprintf(meshfile, "%s.%06d", meshfile_prefix, myid);
284 if ((file = fopen(meshfile, "w")) == NULL)
285 {
286 printf("Error: can't open output file %s\n", meshfile);
287 exit(1);
288 }
289
290 /* mesh header */
291 fprintf(file, "MFEM mesh v1.0\n");
292 fprintf(file, "\ndimension\n");
293 fprintf(file, "%d\n", dim);
294
295 /* mesh elements */
296 fprintf(file, "\nelements\n");
297 fprintf(file, "%d\n", nelem);
298
299 vert = 0;
300 for (p = 0; p < nparts; p++)
301 {
302 part = ((hypre_SStructGrid *)grid)->pgrids[p]->sgrids[0];
303 boxes = hypre_StructGridBoxes(part);
304 for (b = 0; b < hypre_BoxArraySize(boxes); b++)
305 {
306 box = hypre_BoxArrayBox(boxes, b);
307 for (k = hypre_BoxIMinD(box,2); k <= hypre_BoxIMaxD(box,2); k++)
308 for (j = hypre_BoxIMinD(box,1); j <= hypre_BoxIMaxD(box,1); j++)
309 for (i = hypre_BoxIMinD(box,0); i <= hypre_BoxIMaxD(box,0); i++)
310 {
311 fprintf(file, "1 %d ", elemid);
312 for (v = 0; v < cellNV; v++, vert++)
313 fprintf(file, "%d ", vert);
314 fprintf(file, "\n");
315 }
316 }
317 }
318
319 /* boundary will be generated by GLVis */
320 fprintf(file, "\nboundary\n");
321 fprintf(file, "0\n");
322
323 /* mesh vertices */
324 fprintf(file, "\nvertices\n");
325 fprintf(file, "%d\n", nvert);
326 fprintf(file, "%d\n", dim);
327
328 for (p = 0; p < nparts; p++)
329 {
330 part = ((hypre_SStructGrid *)grid)->pgrids[p]->sgrids[0];
331 x0 = y0 = z0 = 0;
332 h = 1.0;
333 boxes = hypre_StructGridBoxes(part);
334 for (b = 0; b < hypre_BoxArraySize(boxes); b++)
335 {
336 box = hypre_BoxArrayBox(boxes, b);
337 for (k = hypre_BoxIMinD(box,2); k <= hypre_BoxIMaxD(box,2); k++)
338 for (j = hypre_BoxIMinD(box,1); j <= hypre_BoxIMaxD(box,1); j++)
339 for (i = hypre_BoxIMinD(box,0); i <= hypre_BoxIMaxD(box,0); i++)
340 if (dim == 2)
341 {
342 if (!given_trans)
343 {
344 fprintf(file, "%.14e %.14e \n", x0+i*h, y0+j*h);
345 fprintf(file, "%.14e %.14e \n", x0+(i+1)*h, y0+j*h);
346 fprintf(file, "%.14e %.14e \n", x0+(i+1)*h, y0+(j+1)*h);
347 fprintf(file, "%.14e %.14e \n", x0+i*h, y0+(j+1)*h);
348 }
349 else
350 {
351 fprintf(file, "%.14e %.14e \n",
352 T[0]*i+T[1]*j+O[0],
353 T[2]*i+T[3]*j+O[1]);
354 fprintf(file, "%.14e %.14e \n",
355 T[0]*(i+1)+T[1]*j+O[0],
356 T[2]*(i+1)+T[3]*j+O[1]);
357 fprintf(file, "%.14e %.14e \n",
358 T[0]*(i+1)+T[1]*(j+1)+O[0],
359 T[2]*(i+1)+T[3]*(j+1)+O[1]);
360 fprintf(file, "%.14e %.14e \n",
361 T[0]*i+T[1]*(j+1)+O[0],
362 T[2]*i+T[3]*(j+1)+O[1]);
363 }
364 }
365 else
366 {
367 if (!given_trans)
368 {
369 fprintf(file, "%.14e %.14e %.14e \n", x0+i*h, y0+j*h, z0+k*h);
370 fprintf(file, "%.14e %.14e %.14e \n", x0+(i+1)*h, y0+j*h, z0+k*h);
371 fprintf(file, "%.14e %.14e %.14e \n", x0+(i+1)*h, y0+(j+1)*h, z0+k*h);
372 fprintf(file, "%.14e %.14e %.14e \n", x0+i*h, y0+(j+1)*h, z0+k*h);
373 fprintf(file, "%.14e %.14e %.14e \n", x0+i*h, y0+j*h, z0+(k+1)*h);
374 fprintf(file, "%.14e %.14e %.14e \n", x0+(i+1)*h, y0+j*h, z0+(k+1)*h);
375 fprintf(file, "%.14e %.14e %.14e \n", x0+(i+1)*h, y0+(j+1)*h, z0+(k+1)*h);
376 fprintf(file, "%.14e %.14e %.14e \n", x0+i*h, y0+(j+1)*h, z0+(k+1)*h);
377 }
378 else
379 {
380 fprintf(file, "%.14e %.14e %.14e \n",
381 T[0]*i+T[1]*j+T[2]*k+O[0],
382 T[3]*i+T[4]*j+T[5]*k+O[1],
383 T[6]*i+T[7]*j+T[8]*k+O[2]);
384 fprintf(file, "%.14e %.14e %.14e \n",
385 T[0]*(i+1)+T[1]*j+T[2]*k+O[0],
386 T[3]*(i+1)+T[4]*j+T[5]*k+O[1],
387 T[6]*(i+1)+T[7]*j+T[8]*k+O[2]);
388 fprintf(file, "%.14e %.14e %.14e \n",
389 T[0]*(i+1)+T[1]*(j+1)+T[2]*k+O[0],
390 T[3]*(i+1)+T[4]*(j+1)+T[5]*k+O[1],
391 T[6]*(i+1)+T[7]*(j+1)+T[8]*k+O[2]);
392 fprintf(file, "%.14e %.14e %.14e \n",
393 T[0]*i+T[1]*(j+1)+T[2]*k+O[0],
394 T[3]*i+T[4]*(j+1)+T[5]*k+O[1],
395 T[6]*i+T[7]*(j+1)+T[8]*k+O[2]);
396 fprintf(file, "%.14e %.14e %.14e \n",
397 T[0]*i+T[1]*j+T[2]*(k+1)+O[0],
398 T[3]*i+T[4]*j+T[5]*(k+1)+O[1],
399 T[6]*i+T[7]*j+T[8]*(k+1)+O[2]);
400 fprintf(file, "%.14e %.14e %.14e \n",
401 T[0]*(i+1)+T[1]*j+T[2]*(k+1)+O[0],
402 T[3]*(i+1)+T[4]*j+T[5]*(k+1)+O[1],
403 T[6]*(i+1)+T[7]*j+T[8]*(k+1)+O[2]);
404 fprintf(file, "%.14e %.14e %.14e \n",
405 T[0]*(i+1)+T[1]*(j+1)+T[2]*(k+1)+O[0],
406 T[3]*(i+1)+T[4]*(j+1)+T[5]*(k+1)+O[1],
407 T[6]*(i+1)+T[7]*(j+1)+T[8]*(k+1)+O[2]);
408 fprintf(file, "%.14e %.14e %.14e \n",
409 T[0]*i+T[1]*(j+1)+T[2]*(k+1)+O[0],
410 T[3]*i+T[4]*(j+1)+T[5]*(k+1)+O[1],
411 T[6]*i+T[7]*(j+1)+T[8]*(k+1)+O[2]);
412 }
413 }
414 }
415
416 if (given_trans)
417 {
418 T += dim*dim;
419 O += dim;
420 }
421 }
422
423 fflush(file);
424 fclose(file);
425 }
426 }
427
428 /* Save a GLVis grid function (in a file with the given prefix) corresponding to
429 the values of the input SStruct vector restricted to the specified SStruct
430 variable. Currently only CELL and NODE variable types are supported. */
431 void GLVis_PrintSStructVector(HYPRE_SStructVector sol,
432 int var,
433 const char *solfile_prefix,
434 int myid)
435 {
436 FILE *file;
437 char solfile[255];
438
439 hypre_SStructGrid *grid = ((hypre_SStructVector*)sol)->grid;
440 int dim = grid->ndim;
441
442 hypre_StructGrid *part;
443 int p, nparts = grid->nparts;
444 hypre_BoxArray *boxes;
445 hypre_Box *box;
446 int b;
447
448 int i, j, k, ni, nj, nk;
449 double *values;
450 int ilower[3], iupper[3];
451
452 HYPRE_SStructVariable vartype = grid->pgrids[0]->vartypes[var];
453
454 char fe_coll[100];
455 int var_off;
456
457 sprintf(solfile, "%s.%06d", solfile_prefix, myid);
458 if ((file = fopen(solfile, "w")) == NULL)
459 {
460 printf("Error: can't open output file %s\n", solfile);
461 exit(1);
462 }
463
464 /* set the finite element collection based on variable type */
465 switch (vartype)
466 {
467 case HYPRE_SSTRUCT_VARIABLE_CELL:
468 sprintf(fe_coll, "%s", "Local_L2_2D_P0");
469 var_off = 0;
470 break;
471 case HYPRE_SSTRUCT_VARIABLE_NODE:
472 sprintf(fe_coll, "%s", "Local_H1_2D_P1");
473 var_off = 1;
474 break;
475 default:
476 printf("Error: unsuported variable type\n");
477 exit(1);
478 }
479
480 /* grid function header */
481 fprintf(file, "FiniteElementSpace\n");
482 fprintf(file, "FiniteElementCollection: %s\n", fe_coll);
483 fprintf(file, "VDim: 1\n");
484 fprintf(file, "Ordering: 0\n\n");
485
486 /* extract and save the vector values on each cell */
487 for (p = 0; p < nparts; p++)
488 {
489 part = grid->pgrids[p]->sgrids[0];
490 boxes = hypre_StructGridBoxes(part);
491 for (b = 0; b < hypre_BoxArraySize(boxes); b++)
492 {
493 box = hypre_BoxArrayBox(boxes, b);
494 ni = hypre_BoxSizeD(box,0);
495 nj = hypre_BoxSizeD(box,1);
496 nk = hypre_BoxSizeD(box,2);
497
498 ilower[0] = hypre_BoxIMinD(box,0) - var_off;
499 ilower[1] = hypre_BoxIMinD(box,1) - var_off;
500 iupper[0] = hypre_BoxIMaxD(box,0);
501 iupper[1] = hypre_BoxIMaxD(box,1);
502
503 if (dim == 2)
504 values = (double*) malloc((ni+var_off)*(nj+var_off)*sizeof(double));
505 else
506 {
507 values = (double*) malloc((ni+var_off)*(nj+var_off)*(nk+var_off)*sizeof(double));
508 ilower[2] = hypre_BoxIMinD(box,2) - var_off;
509 iupper[2] = hypre_BoxIMaxD(box,2);
510 }
511
512 HYPRE_SStructVectorGetBoxValues(sol, p, ilower, iupper, var, values);
513
514 if (vartype == HYPRE_SSTRUCT_VARIABLE_CELL)
515 {
516 for (k = 0; k < nk; k++)
517 for (j = 0; j < nj; j++)
518 for (i = 0; i < ni; i++)
519 fprintf(file, "%.14e\n", values[i + j*ni]);
520 }
521 else if (vartype == HYPRE_SSTRUCT_VARIABLE_NODE)
522 {
523 if (dim == 2)
524 {
525 for (j = 0; j < nj; j++)
526 for (i = 0; i < ni; i++)
527 {
528 fprintf(file, "%.14e\n", values[i + j*(ni+1)]);
529 fprintf(file, "%.14e\n", values[i+1 + j*(ni+1)]);
530 fprintf(file, "%.14e\n", values[i+1 + (j+1)*(ni+1)]);
531 fprintf(file, "%.14e\n", values[i + (j+1)*(ni+1)]);
532 }
533 }
534 else
535 {
536 for (k = 0; k < nk; k++)
537 for (j = 0; j < nj; j++)
538 for (i = 0; i < ni; i++)
539 {
540 fprintf(file, "%.14e\n", values[i + j*(ni+1) + k*(ni+1)*(nj+1)]);
541 fprintf(file, "%.14e\n", values[i+1 + j*(ni+1) + k*(ni+1)*(nj+1)]);
542 fprintf(file, "%.14e\n", values[i+1 + (j+1)*(ni+1) + k*(ni+1)*(nj+1)]);
543 fprintf(file, "%.14e\n", values[i + (j+1)*(ni+1) + k*(ni+1)*(nj+1)]);
544 fprintf(file, "%.14e\n", values[i + j*(ni+1) + (k+1)*(ni+1)*(nj+1)]);
545 fprintf(file, "%.14e\n", values[i+1 + j*(ni+1) + (k+1)*(ni+1)*(nj+1)]);
546 fprintf(file, "%.14e\n", values[i+1 + (j+1)*(ni+1) + (k+1)*(ni+1)*(nj+1)]);
547 fprintf(file, "%.14e\n", values[i + (j+1)*(ni+1) + (k+1)*(ni+1)*(nj+1)]);
548 }
549 }
550 }
551
552 free(values);
553 }
554 }
555
556 fflush(file);
557 fclose(file);
558 }
559
560 /* Save a GLVis mesh file with the given prefix corresponding to the input
561 Struct grid assuming that the cells are the same. The optional trans and
562 origin parameters specify a coordinate transformation, relative to a square
563 Cartesian grid. */
564 void GLVis_PrintStructGrid(HYPRE_StructGrid Grid,
565 const char *meshfile_prefix, int myid,
566 double *trans, double *origin)
567 {
568 FILE *file;
569 char meshfile[255];
570
571 hypre_StructGrid *grid = (hypre_StructGrid *)Grid;
572 int dim = grid->ndim;
573 int cellNV = (dim == 2) ? 4 : 8;
574 int elemid = 2*dim-1;
575 int nvert, nelem;
576
577 int given_trans = (trans != NULL && origin != NULL);
578 double *T = trans, *O = origin;
579
580 hypre_BoxArray *boxes;
581 hypre_Box *box;
582 int b, ncells;
583
584 nvert = nelem = 0;
585 boxes = hypre_StructGridBoxes(grid);
586 for (b = 0; b < hypre_BoxArraySize(boxes); b++)
587 {
588 box = hypre_BoxArrayBox(boxes, b);
589 ncells = hypre_BoxVolume(box);
590 nvert += ncells*cellNV;
591 nelem += ncells;
592 }
593
594 {
595 int i, j, k, v, vert;
596 double x0, y0, z0, h;
597
598 sprintf(meshfile, "%s.%06d", meshfile_prefix, myid);
599 if ((file = fopen(meshfile, "w")) == NULL)
600 {
601 printf("Error: can't open output file %s\n", meshfile);
602 exit(1);
603 }
604
605 /* mesh header */
606 fprintf(file, "MFEM mesh v1.0\n");
607 fprintf(file, "\ndimension\n");
608 fprintf(file, "%d\n", dim);
609
610 /* mesh elements */
611 fprintf(file, "\nelements\n");
612 fprintf(file, "%d\n", nelem);
613
614 vert = 0;
615
616 boxes = hypre_StructGridBoxes(grid);
617 for (b = 0; b < hypre_BoxArraySize(boxes); b++)
618 {
619 box = hypre_BoxArrayBox(boxes, b);
620 for (k = hypre_BoxIMinD(box,2); k <= hypre_BoxIMaxD(box,2); k++)
621 for (j = hypre_BoxIMinD(box,1); j <= hypre_BoxIMaxD(box,1); j++)
622 for (i = hypre_BoxIMinD(box,0); i <= hypre_BoxIMaxD(box,0); i++)
623 {
624 fprintf(file, "1 %d ", elemid);
625 for (v = 0; v < cellNV; v++, vert++)
626 fprintf(file, "%d ", vert);
627 fprintf(file, "\n");
628 }
629 }
630
631 /* boundary will be generated by GLVis */
632 fprintf(file, "\nboundary\n");
633 fprintf(file, "0\n");
634
635 /* mesh vertices */
636 fprintf(file, "\nvertices\n");
637 fprintf(file, "%d\n", nvert);
638 fprintf(file, "%d\n", dim);
639
640 x0 = y0 = z0 = 0;
641 h = 1.0;
642 boxes = hypre_StructGridBoxes(grid);
643 for (b = 0; b < hypre_BoxArraySize(boxes); b++)
644 {
645 box = hypre_BoxArrayBox(boxes, b);
646 for (k = hypre_BoxIMinD(box,2); k <= hypre_BoxIMaxD(box,2); k++)
647 for (j = hypre_BoxIMinD(box,1); j <= hypre_BoxIMaxD(box,1); j++)
648 for (i = hypre_BoxIMinD(box,0); i <= hypre_BoxIMaxD(box,0); i++)
649 if (dim == 2)
650 {
651 if (!given_trans)
652 {
653 fprintf(file, "%.14e %.14e \n", x0+i*h, y0+j*h);
654 fprintf(file, "%.14e %.14e \n", x0+(i+1)*h, y0+j*h);
655 fprintf(file, "%.14e %.14e \n", x0+(i+1)*h, y0+(j+1)*h);
656 fprintf(file, "%.14e %.14e \n", x0+i*h, y0+(j+1)*h);
657 }
658 else
659 {
660 fprintf(file, "%.14e %.14e \n",
661 T[0]*i+T[1]*j+O[0],
662 T[2]*i+T[3]*j+O[1]);
663 fprintf(file, "%.14e %.14e \n",
664 T[0]*(i+1)+T[1]*j+O[0],
665 T[2]*(i+1)+T[3]*j+O[1]);
666 fprintf(file, "%.14e %.14e \n",
667 T[0]*(i+1)+T[1]*(j+1)+O[0],
668 T[2]*(i+1)+T[3]*(j+1)+O[1]);
669 fprintf(file, "%.14e %.14e \n",
670 T[0]*i+T[1]*(j+1)+O[0],
671 T[2]*i+T[3]*(j+1)+O[1]);
672 }
673 }
674 else
675 {
676 if (!given_trans)
677 {
678 fprintf(file, "%.14e %.14e %.14e \n", x0+i*h, y0+j*h, z0+k*h);
679 fprintf(file, "%.14e %.14e %.14e \n", x0+(i+1)*h, y0+j*h, z0+k*h);
680 fprintf(file, "%.14e %.14e %.14e \n", x0+(i+1)*h, y0+(j+1)*h, z0+k*h);
681 fprintf(file, "%.14e %.14e %.14e \n", x0+i*h, y0+(j+1)*h, z0+k*h);
682 fprintf(file, "%.14e %.14e %.14e \n", x0+i*h, y0+j*h, z0+(k+1)*h);
683 fprintf(file, "%.14e %.14e %.14e \n", x0+(i+1)*h, y0+j*h, z0+(k+1)*h);
684 fprintf(file, "%.14e %.14e %.14e \n", x0+(i+1)*h, y0+(j+1)*h, z0+(k+1)*h);
685 fprintf(file, "%.14e %.14e %.14e \n", x0+i*h, y0+(j+1)*h, z0+(k+1)*h);
686 }
687 else
688 {
689 fprintf(file, "%.14e %.14e %.14e \n",
690 T[0]*i+T[1]*j+T[2]*k+O[0],
691 T[3]*i+T[4]*j+T[5]*k+O[1],
692 T[6]*i+T[7]*j+T[8]*k+O[2]);
693 fprintf(file, "%.14e %.14e %.14e \n",
694 T[0]*(i+1)+T[1]*j+T[2]*k+O[0],
695 T[3]*(i+1)+T[4]*j+T[5]*k+O[1],
696 T[6]*(i+1)+T[7]*j+T[8]*k+O[2]);
697 fprintf(file, "%.14e %.14e %.14e \n",
698 T[0]*(i+1)+T[1]*(j+1)+T[2]*k+O[0],
699 T[3]*(i+1)+T[4]*(j+1)+T[5]*k+O[1],
700 T[6]*(i+1)+T[7]*(j+1)+T[8]*k+O[2]);
701 fprintf(file, "%.14e %.14e %.14e \n",
702 T[0]*i+T[1]*(j+1)+T[2]*k+O[0],
703 T[3]*i+T[4]*(j+1)+T[5]*k+O[1],
704 T[6]*i+T[7]*(j+1)+T[8]*k+O[2]);
705 fprintf(file, "%.14e %.14e %.14e \n",
706 T[0]*i+T[1]*j+T[2]*(k+1)+O[0],
707 T[3]*i+T[4]*j+T[5]*(k+1)+O[1],
708 T[6]*i+T[7]*j+T[8]*(k+1)+O[2]);
709 fprintf(file, "%.14e %.14e %.14e \n",
710 T[0]*(i+1)+T[1]*j+T[2]*(k+1)+O[0],
711 T[3]*(i+1)+T[4]*j+T[5]*(k+1)+O[1],
712 T[6]*(i+1)+T[7]*j+T[8]*(k+1)+O[2]);
713 fprintf(file, "%.14e %.14e %.14e \n",
714 T[0]*(i+1)+T[1]*(j+1)+T[2]*(k+1)+O[0],
715 T[3]*(i+1)+T[4]*(j+1)+T[5]*(k+1)+O[1],
716 T[6]*(i+1)+T[7]*(j+1)+T[8]*(k+1)+O[2]);
717 fprintf(file, "%.14e %.14e %.14e \n",
718 T[0]*i+T[1]*(j+1)+T[2]*(k+1)+O[0],
719 T[3]*i+T[4]*(j+1)+T[5]*(k+1)+O[1],
720 T[6]*i+T[7]*(j+1)+T[8]*(k+1)+O[2]);
721 }
722 }
723
724 if (given_trans)
725 {
726 T += dim*dim;
727 O += dim;
728 }
729 }
730
731 fflush(file);
732 fclose(file);
733 }
734 }
735
736 /* Save a Q0 GLVis grid function (in a file with the given prefix) corresponding
737 to the values of the input Struct vector. */
738 void GLVis_PrintStructVector(HYPRE_StructVector sol,
739 const char *solfile_prefix,
740 int myid)
741 {
742 FILE *file;
743 char solfile[255];
744
745 hypre_StructGrid *grid = ((hypre_StructVector*)sol)->grid;
746 int dim = grid->ndim;
747
748 hypre_BoxArray *boxes;
749 hypre_Box *box;
750 int b;
751
752 int i, j, k, ni, nj, nk;
753 double *values;
754 int ilower[3], iupper[3];
755
756 sprintf(solfile, "%s.%06d", solfile_prefix, myid);
757 if ((file = fopen(solfile, "w")) == NULL)
758 {
759 printf("Error: can't open output file %s\n", solfile);
760 exit(1);
761 }
762
763 /* grid function header */
764 fprintf(file, "FiniteElementSpace\n");
765 fprintf(file, "FiniteElementCollection: Local_L2_2D_P0\n");
766 fprintf(file, "VDim: 1\n");
767 fprintf(file, "Ordering: 0\n\n");
768
769 /* extract and save the vector values on each cell */
770 boxes = hypre_StructGridBoxes(grid);
771 for (b = 0; b < hypre_BoxArraySize(boxes); b++)
772 {
773 box = hypre_BoxArrayBox(boxes, b);
774 ni = hypre_BoxSizeD(box,0);
775 nj = hypre_BoxSizeD(box,1);
776 nk = hypre_BoxSizeD(box,2);
777
778 ilower[0] = hypre_BoxIMinD(box,0);
779 ilower[1] = hypre_BoxIMinD(box,1);
780 iupper[0] = hypre_BoxIMaxD(box,0);
781 iupper[1] = hypre_BoxIMaxD(box,1);
782
783 if (dim == 2)
784 values = (double*) malloc(ni*nj*sizeof(double));
785 else
786 {
787 values = (double*) malloc(ni*nj*nk*sizeof(double));
788 ilower[2] = hypre_BoxIMinD(box,2);
789 iupper[2] = hypre_BoxIMaxD(box,2);
790 }
791
792 HYPRE_StructVectorGetBoxValues(sol, ilower, iupper, values);
793
794 for (k = 0; k < nk; k++)
795 for (j = 0; j < nj; j++)
796 for (i = 0; i < ni; i++)
797 fprintf(file, "%.14e\n", values[i + j*ni]);
798
799 free(values);
800 }
801
802 fflush(file);
803 fclose(file);
804 }
805
806 /* Save additional data needed for GLVis visualization (e.g. the number of
807 processors in the run). */
808 void GLVis_PrintData(const char *datafile, int myid, int num_procs)
809 {
810 FILE *file;
811
812 if (myid == 0)
813 {
814 file = fopen(datafile, "w");
815 fprintf(file, "np %d\n", num_procs);
816 fflush(file);
817 fclose(file);
818 }
819 }
syntax highlighted by Code2HTML, v. 0.9.1