1 /*   Program write_grid_str.c    */
2 /*
3 Creates simple 3-D structured grid and writes it to a
4 CGNS file.
5 
6 Example compilation for this program is (change paths if needed!):
7 
8 cc -I ../.. -c write_grid_str.c
9 cc -o write_grid_str_c write_grid_str.o -L ../../lib -lcgns
10 
11 (../../lib is the location where the compiled
12 library libcgns.a is located)
13 */
14 
15 #include <stdio.h>
16 #include <string.h>
17 /* cgnslib.h file must be located in directory specified by -I during compile: */
18 #include "cgnslib.h"
19 
20 #if CGNS_VERSION < 3100
21 # define cgsize_t int
22 #endif
23 
main()24 int main()
25 {
26 /*
27    dimension statements (note that tri-dimensional arrays
28    x,y,z must be dimensioned exactly as [N][17][21] (N>=9)
29    for this particular case or else they will be written to
30    the CGNS file incorrectly!  Other options are to use
31    cg_coord_general_write, 1-D arrays, use dynamic memory,
32    or pass index values to a subroutine and dimension exactly
33    there):
34 */
35    double x[9][17][21],y[9][17][21],z[9][17][21];
36    cgsize_t isize[3][3];
37    int ni,nj,nk,i,j,k;
38    int index_file,icelldim,iphysdim,index_base;
39    int index_zone,index_coord;
40    char basename[33],zonename[33];
41 
42    printf("\nProgram write_grid_str\n");
43 
44 /* create gridpoints for simple example: */
45    ni=21;
46    nj=17;
47    nk=9;
48    for (k=0; k < nk; ++k)
49    {
50      for (j=0; j < nj; ++j)
51      {
52        for (i=0; i < ni; ++i)
53        {
54          x[k][j][i]=i;
55          y[k][j][i]=j;
56          z[k][j][i]=k;
57        }
58      }
59    }
60    printf("\ncreated simple 3-D grid points");
61 
62 /* WRITE X, Y, Z GRID POINTS TO CGNS FILE */
63 /* open CGNS file for write */
64    if (cg_open("grid_c.cgns",CG_MODE_WRITE,&index_file)) cg_error_exit();
65 /* create base (user can give any name) */
66    strcpy(basename,"Base");
67    icelldim=3;
68    iphysdim=3;
69    cg_base_write(index_file,basename,icelldim,iphysdim,&index_base);
70 /* define zone name (user can give any name) */
71    strcpy(zonename,"Zone  1");
72 /* vertex size */
73    isize[0][0]=21;
74    isize[0][1]=17;
75    isize[0][2]=9;
76 /* cell size */
77    isize[1][0]=isize[0][0]-1;
78    isize[1][1]=isize[0][1]-1;
79    isize[1][2]=isize[0][2]-1;
80 /* boundary vertex size (always zero for structured grids) */
81    isize[2][0]=0;
82    isize[2][1]=0;
83    isize[2][2]=0;
84 /* create zone */
85    cg_zone_write(index_file,index_base,zonename,*isize,CGNS_ENUMV(Structured),&index_zone);
86 /* write grid coordinates (user must use SIDS-standard names here) */
87    cg_coord_write(index_file,index_base,index_zone,CGNS_ENUMV(RealDouble),"CoordinateX",
88        x,&index_coord);
89    cg_coord_write(index_file,index_base,index_zone,CGNS_ENUMV(RealDouble),"CoordinateY",
90        y,&index_coord);
91    cg_coord_write(index_file,index_base,index_zone,CGNS_ENUMV(RealDouble),"CoordinateZ",
92        z,&index_coord);
93 /* close CGNS file */
94    cg_close(index_file);
95    printf("\nSuccessfully wrote grid to file grid_c.cgns\n");
96    return 0;
97 }
98