1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #ifdef _WIN32
6 # include <io.h>
7 # define unlink _unlink
8 #else
9 # include <unistd.h>
10 #endif
11 #include "utils.h"
12 
13 char *fname = "bcdata.cgns";
14 
15 int cgfile, cgbase, cgzone, cgcoord, cgsect, cgbc, cgfam, cgdset;
16 char name[33];
17 
18 cgsize_t size[] = {4, 1, 0};
19 float coord[3][4] = {
20     {0, 1, 0, 0},
21     {0, 0, 1, 0},
22     {0, 0, 0, 1}
23 };
24 cgsize_t tris[12] = {
25     1, 3, 2,
26     1, 2, 4,
27     1, 4, 3,
28     2, 3, 4
29 };
30 cgsize_t tets[4] = {1, 2, 3, 4};
31 
32 cgsize_t dim, npnts = 4;
33 cgsize_t pnts[4] = {1, 2, 3, 4};
34 float data[4] = {1, 2, 3, 4};
35 
36 float start, finish;
37 
main(int argc,char ** argv)38 int main (int argc, char **argv)
39 {
40     int i, j, nb = 5, nv = 5;
41     float exps[5];
42     char *endptr;
43 
44     if (argc > 1) {
45         /* Get safely input values */
46         long input_value;
47         errno = 0;
48         input_value = strtol(argv[1], &endptr, 10);
49         if (errno == ERANGE){
50             fprintf (stderr, "overflow when converting nb input to int\n");
51             exit(1);
52         }
53         if (endptr == argv[1]){
54             fprintf (stderr, "impossible to convert nb input to int\n");
55             exit(1);
56         }
57         else {
58             nb = (int) input_value;
59         }
60         if (argc > 2){
61             errno = 0;
62             input_value = strtol(argv[2], &endptr, 10);
63             if (errno == ERANGE){
64                 fprintf (stderr, "overflow when converting nv input to int\n");
65                 exit(1);
66             }
67             if (endptr == argv[2]){
68                 fprintf (stderr, "impossible to convert nv input to int\n");
69                 exit(1);
70             }
71             else {
72                 nv = (int) input_value;
73             }
74 	}
75     }
76     for (i = 0; i < 5; i++)
77         exps[i] = (float)0.0;
78     exps[1] = (float)1.0;
79 
80     unlink (fname);
81     printf ("creating file ...");
82     fflush (stdout);
83     start = (float)elapsed_time();
84 
85     if (cg_open (fname, CG_MODE_WRITE, &cgfile) ||
86         cg_base_write (cgfile, "Base", 3, 3, &cgbase) ||
87         cg_goto(cgfile, cgbase, NULL) ||
88         cg_dataclass_write(CGNS_ENUMV(NormalizedByUnknownDimensional)) ||
89         cg_zone_write (cgfile, cgbase, "Zone", size,
90             CGNS_ENUMV(Unstructured), &cgzone) ||
91         cg_coord_write (cgfile, cgbase, cgzone, CGNS_ENUMV(RealSingle),
92             "CoordinateX", coord[0], &cgcoord) ||
93         cg_gopath(cgfile, "/Base/Zone/GridCoordinates/CoordinateX") ||
94         cg_exponents_write(CGNS_ENUMV(RealSingle), exps) ||
95         cg_coord_write (cgfile, cgbase, cgzone, CGNS_ENUMV(RealSingle),
96             "CoordinateY", coord[1], &cgcoord) ||
97         cg_gopath(cgfile, "../CoordinateY") ||
98         cg_exponents_write(CGNS_ENUMV(RealSingle), exps) ||
99         cg_coord_write (cgfile, cgbase, cgzone, CGNS_ENUMV(RealSingle),
100             "CoordinateZ", coord[2], &cgcoord) ||
101         cg_gopath(cgfile, "../CoordinateZ") ||
102         cg_exponents_write(CGNS_ENUMV(RealSingle), exps) ||
103         cg_section_write (cgfile, cgbase, cgzone, "Tris",
104             CGNS_ENUMV(TRI_3), 1, 4, 0, tris, &cgsect) ||
105         cg_section_write (cgfile, cgbase, cgzone, "Tets",
106             CGNS_ENUMV(TETRA_4), 5, 5, 0, tets, &cgsect))
107         cg_error_exit();
108 
109     dim = npnts;
110     for (j = 1; j <= nb; j++) {
111         sprintf (name, "BC%d", j);
112         if (cg_boco_write (cgfile, cgbase, cgzone, name, CGNS_ENUMV(BCWall),
113                 CGNS_ENUMV(ElementList), npnts, pnts, &cgbc))
114             cg_error_exit();
115 
116         for (i = 1; i <= nv; i++) {
117             sprintf (name, "BCData%d", i);
118             if (cg_dataset_write (cgfile, cgbase, cgzone, cgbc, name,
119 				  CGNS_ENUMV(BCWall), &cgdset) ||
120                 cg_bcdata_write (cgfile, cgbase, cgzone, cgbc, cgdset,
121 				 CGNS_ENUMV(Dirichlet)) ||
122                 cg_goto (cgfile, cgbase, "Zone_t", 1, "ZoneBC_t", 1,
123                     "BC_t", cgbc, "BCDataSet_t", cgdset, "end") ||
124                 cg_descriptor_write ("label", name) ||
125                 cg_descriptor_write ("basis", "intensive") ||
126                 cg_goto (cgfile, cgbase, "Zone_t", 1, "ZoneBC_t", 1,
127                     "BC_t", cgbc, "BCDataSet_t", cgdset,
128 		    "BCData_t", CGNS_ENUMV(Dirichlet), "end") ||
129                 cg_array_write ("Data", CGNS_ENUMV(RealSingle), 1, &dim, data))
130                 cg_error_exit();
131         }
132     }
133 
134     dim = 1;
135     for (j = 1; j <= nb; j++) {
136         sprintf (name, "Family%d", j);
137         if (cg_family_write (cgfile, cgbase, name, &cgfam) ||
138             cg_fambc_write (cgfile, cgbase, cgfam, "FamilyBC",
139                 CGNS_ENUMV(BCWall), &cgbc))
140             cg_error_exit();
141 
142         for (i = 1; i <= nv; i++) {
143             sprintf (name, "FamilyBCData%d", i);
144             if (cg_goto (cgfile, cgbase, "Family_t", j, "FamilyBC_t", 1, NULL) ||
145                 cg_bcdataset_write (name, CGNS_ENUMV(BCWall),
146                     CGNS_ENUMV(Dirichlet)) ||
147                 cg_gorel (cgfile, "FamilyBCDataSet_t", i, NULL) ||
148                 cg_descriptor_write ("label", name) ||
149                 cg_descriptor_write ("basis", "intensive") ||
150                 cg_gorel (cgfile, "DirichletData", 0, NULL) ||
151                 cg_array_write ("Data", CGNS_ENUMV(RealSingle), 1, &dim, data))
152                 cg_error_exit();
153         }
154     }
155 
156     finish = (float)elapsed_time();
157     printf (" %.2f secs\n", finish - start);
158 
159     printf ("closing file  ...");
160     fflush (stdout);
161     start = (float)elapsed_time();
162     if (cg_close(cgfile)) cg_error_exit();
163     finish = (float)elapsed_time();
164     printf (" %.2f secs\n", finish - start);
165 
166     printf ("opening file  ...");
167     fflush (stdout);
168     start = (float)elapsed_time();
169     if (cg_open (fname, CG_MODE_MODIFY, &cgfile)) cg_error_exit();
170     finish = (float)elapsed_time();
171     printf (" %.2f secs\n", finish - start);
172     cg_close (cgfile);
173 
174     return 0;
175 }
176 
177