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 }


syntax highlighted by Code2HTML, v. 0.9.1