00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #ifndef _WIN32
00005 # include <unistd.h>
00006 #endif
00007 #include "cgnslib.h"
00008
00009 #define NUM_SIDE 5
00010
00011 float coord[NUM_SIDE*NUM_SIDE*NUM_SIDE];
00012
00013 #define CHECK(L,B) if(!(B)){fprintf(stderr,"mismatch in %s\n",L);exit(1);}
00014
00015 void error_exit (int iserr, char *msg)
00016 {
00017 if (iserr) {
00018 printf ("ERROR:%s\n", msg);
00019 exit (1);
00020 }
00021 else
00022 printf ("WARNING:%s\n", msg);
00023 }
00024
00025 int main (int argc, char **argv)
00026 {
00027 int n, i, j, k, nuser, dim = 1;
00028 int cgfile, cgbase, cgzone, cgcoord, cgdset;
00029 int size[9];
00030 int ptlist[3] = {1, 2, 3};
00031 int ptrange[6] = {1, 1, 1, 2, 2, 2};
00032 int bcpoints[6], bcfaces[6];
00033 static char *fname = "gotest.cgns";
00034 char name[33];
00035 float data1 = 1;
00036 float data2 = 2;
00037 float exponents[8], rate[3], center[3];
00038 GridLocation_t gridloc;
00039 int ordinal, ndata, cgfam, cgbc, nunits, nexps;
00040 int elecflag, magnflag, condflag, dirichlet, neumann;
00041 PointSetType_t pttype;
00042 DataClass_t dclass;
00043 DataType_t dtype;
00044 BCType_t bctype;
00045 MassUnits_t mass;
00046 LengthUnits_t length;
00047 TimeUnits_t time;
00048 TemperatureUnits_t temp;
00049 AngleUnits_t angle;
00050 ElectricCurrentUnits_t current;
00051 SubstanceAmountUnits_t amount;
00052 LuminousIntensityUnits_t intensity;
00053 ModelType_t elecmodel;
00054 ModelType_t magnmodel;
00055 ModelType_t emconduct;
00056
00057
00058
00059 cg_configure(CG_CONFIG_ERROR, (void *)error_exit);
00060
00061 if (argc > 1) {
00062 n = 0;
00063 if (argv[1][n] == '-') n++;
00064 if (argv[1][n] == 'a' || argv[1][n] == 'A')
00065 i = CG_FILE_ADF;
00066 else if (argv[1][n] == 'h' || argv[1][n] == 'H')
00067 i = CG_FILE_HDF5;
00068 else if (argv[1][n] == 'x' || argv[1][n] == 'X')
00069 i = CG_FILE_XML;
00070 else {
00071 fprintf(stderr, "unknown option\n");
00072 exit (1);
00073 }
00074 cg_set_file_type(i);
00075 }
00076
00077 for (n = 0; n < 8; n++)
00078 exponents[n] = (float)n;
00079 for (n = 0; n < 3; n++) {
00080 rate[n] = (float)n;
00081 center[n] = (float)n;
00082 }
00083 for (n = 0; n < NUM_SIDE*NUM_SIDE*NUM_SIDE; n++)
00084 coord[n] = (float)n;
00085
00086 unlink (fname);
00087 printf ("creating CGNS file %s\n", fname);
00088 cg_open (fname, CG_MODE_WRITE, &cgfile);
00089 cg_base_write (cgfile, "Base", 3, 3, &cgbase);
00090
00091
00092
00093 puts ("writing electromagnetics");
00094 cg_goto(cgfile, cgbase, NULL);
00095 cg_equationset_write (3);
00096 cg_goto(cgfile, cgbase, "FlowEquationSet_t", 1, NULL);
00097 cg_model_write("EMElectricFieldModel_t", Voltage);
00098 cg_model_write("EMMagneticFieldModel_t", Interpolated);
00099 cg_model_write("EMConductivityModel_t", Equilibrium_LinRessler);
00100
00101
00102
00103 puts ("writing family/rotating");
00104 cg_family_write(cgfile, cgbase, "Family", &cgfam);
00105
00106 cg_goto(cgfile, cgbase, "Family", 0, NULL);
00107 cg_rotating_write (rate, center);
00108
00109
00110
00111 puts("writing FamilyBCDataSet");
00112 cg_fambc_write(cgfile, cgbase, cgfam, "FamilyBC", BCWall, &cgbc);
00113
00114 cg_gorel(cgfile, "FamilyBC_t", cgbc, NULL);
00115 cg_bcdataset_write ("FamilyBCDataSet", BCWallInviscid, Dirichlet);
00116
00117
00118
00119 puts("writing user defined data under base");
00120
00121 cg_gopath (cgfile, "../..");
00122 cg_user_data_write ("User");
00123
00124 cg_gopath (cgfile, "/Base/User");
00125 cg_gridlocation_write (CellCenter);
00126 cg_famname_write ("Family");
00127 cg_ordinal_write (0);
00128 cg_array_write ("Data1", RealSingle, 1, &dim, &data1);
00129 cg_array_write ("Data2", RealSingle, 1, &dim, &data2);
00130
00131 for (n = 1; n <= 2; n++) {
00132 cg_goto (cgfile, cgbase, "User", 0, "DataArray_t", n, "end");
00133 cg_dataclass_write (Dimensional);
00134 cg_units_write (Kilogram, Meter, Second, Kelvin, Radian);
00135 cg_exponents_write (RealSingle, exponents);
00136 }
00137
00138
00139
00140
00141 cg_configure(CG_CONFIG_ERROR, NULL);
00142 if (cg_ptset_write (PointList, 1, ptlist) == CG_OK)
00143 printf ("WHAT!! - ptset should not work under base/userdata\n");
00144 cg_configure(CG_CONFIG_ERROR, (void *)error_exit);
00145
00146
00147
00148 puts("writing zone");
00149 for (n = 0; n < 3; n++) {
00150 size[n] = NUM_SIDE;
00151 size[n+3] = NUM_SIDE - 1;
00152 size[n+6] = 0;
00153 }
00154 cg_zone_write (cgfile, cgbase, "Zone", size, Structured, &cgzone);
00155 cg_coord_write(cgfile, cgbase, cgzone, RealSingle,
00156 "CoordinateX", coord, &cgcoord);
00157 cg_coord_write(cgfile, cgbase, cgzone, RealSingle,
00158 "CoordinateY", coord, &cgcoord);
00159 cg_coord_write(cgfile, cgbase, cgzone, RealSingle,
00160 "CoordinateZ", coord, &cgcoord);
00161
00162
00163
00164 puts("writing Dirichlet BC with vertex range");
00165 for (n = 0; n < 3; n++) {
00166 bcpoints[n] = 1;
00167 bcpoints[n+3] = NUM_SIDE;
00168 bcfaces[n] = 1;
00169 bcfaces[n+3] = NUM_SIDE - 1;
00170 }
00171 bcpoints[5] = bcfaces[5] = 1;
00172 cg_boco_write (cgfile, cgbase, cgzone, "BC", BCWall,
00173 PointList, 1, bcpoints, &cgbc);
00174 cg_dataset_write (cgfile, cgbase, cgzone, cgbc,
00175 "DataSet", BCWallViscous, &cgdset);
00176 cg_bcdata_write (cgbase, cgfile, cgzone, cgbc, cgdset, Dirichlet);
00177
00178
00179
00180 puts("writing Dirichlet data at faces");
00181 cg_gopath (cgfile, "/Base/Zone/ZoneBC/BC/DataSet");
00182 cg_gridlocation_write (KFaceCenter);
00183 cg_ptset_write (PointRange, 2, bcfaces);
00184
00185 size[0] = (NUM_SIDE - 1) * (NUM_SIDE - 1);
00186 cg_gorel (cgfile, "BCData_t", Dirichlet, NULL);
00187 cg_array_write ("Data", RealSingle, 1, size, coord);
00188
00189
00190
00191 puts("writing recursive user defined data under zone");
00192 cg_goto(cgfile, cgbase, "Zone", 0, NULL);
00193 for (i = 1; i <= 4; i++) {
00194 sprintf (name, "User%d", i);
00195 cg_user_data_write (name);
00196 cg_gorel(cgfile, name, 0, NULL);
00197 cg_gridlocation_write (CellCenter);
00198 cg_famname_write ("Family");
00199 cg_ordinal_write (i);
00200 cg_ptset_write (PointList, 1, ptlist);
00201 cg_array_write ("Data1", RealSingle, 1, &dim, &data1);
00202 cg_array_write ("Data2", RealSingle, 1, &dim, &data2);
00203 for (n = 1; n <= 2; n++) {
00204 cg_gorel (cgfile, "DataArray_t", n, "end");
00205 cg_dataclass_write (Dimensional);
00206 cg_unitsfull_write (Kilogram, Meter, Second, Kelvin, Radian,
00207 Ampere, Mole, Candela);
00208 cg_expfull_write (RealSingle, exponents);
00209 cg_gopath (cgfile, "..");
00210 }
00211
00212 for (j = 1; j <= 3; j++) {
00213 sprintf (name, "User%d.%d", i, j);
00214 cg_user_data_write (name);
00215 cg_gopath (cgfile, name);
00216 cg_gridlocation_write (Vertex);
00217 cg_famname_write ("Family");
00218 cg_ordinal_write (i + j);
00219 cg_ptset_write (PointRange, 2, ptrange);
00220 cg_array_write ("Data1", RealSingle, 1, &dim, &data1);
00221 cg_array_write ("Data2", RealSingle, 1, &dim, &data2);
00222 for (n = 1; n <= 2; n++) {
00223 cg_gorel (cgfile, "DataArray_t", n, "end");
00224 cg_dataclass_write (Dimensional);
00225 cg_unitsfull_write (Kilogram, Meter, Second, Kelvin,
00226 Radian, Ampere, Mole, Candela);
00227 cg_expfull_write (RealSingle, exponents);
00228 cg_gorel (cgfile, "..", 0, NULL);
00229 }
00230
00231 for (k = 1; k <= 2; k++) {
00232 sprintf (name, "User%d.%d.%d", i, j, k);
00233 cg_user_data_write (name);
00234 cg_gorel (cgfile, name, 0, NULL);
00235 cg_array_write ("Data1", RealSingle, 1, &dim, &data1);
00236 cg_array_write ("Data2", RealSingle, 1, &dim, &data2);
00237 for (n = 1; n <= 2; n++) {
00238 cg_gorel (cgfile, "DataArray_t", n, "end");
00239 cg_dataclass_write (Dimensional);
00240 cg_unitsfull_write (Kilogram, Meter, Second, Kelvin,
00241 Radian, Ampere, Mole, Candela);
00242 cg_expfull_write (RealSingle, exponents);
00243 cg_gopath (cgfile, "..");
00244 }
00245
00246 for (n = 1; n <= 2; n++) {
00247 sprintf (name, "User%d.%d.%d.%d", i, j, k, n);
00248 cg_user_data_write (name);
00249 cg_gopath (cgfile, name);
00250 cg_array_write ("Data1", RealSingle, 1, &dim, &data1);
00251 cg_array_write ("Data2", RealSingle, 1, &dim, &data2);
00252 cg_gopath (cgfile, "..");
00253 }
00254 cg_gopath (cgfile, "..");
00255 }
00256 cg_gopath (cgfile, "..");
00257 }
00258 cg_gopath (cgfile, "..");
00259 }
00260
00261 puts ("closing and reopening in read mode");
00262 cg_close (cgfile);
00263
00264
00265
00266 cg_configure(CG_CONFIG_ERROR, NULL);
00267
00268 if (cg_open (fname, CG_MODE_READ, &cgfile))
00269 cg_error_exit ();
00270 cgbase = cgzone = 1;
00271
00272
00273
00274 puts ("checking electromagnetics");
00275 if (cg_goto(cgfile, cgbase, NULL) ||
00276 cg_equationset_elecmagn_read(&elecflag, &magnflag, &condflag) ||
00277 cg_goto(cgfile, cgbase, "FlowEquationSet_t", 1, NULL) ||
00278 cg_model_read ("EMElectricFieldModel_t", &elecmodel) ||
00279 cg_model_read ("EMMagneticFieldModel_t", &magnmodel) ||
00280 cg_model_read ("EMConductivityModel_t", &emconduct))
00281 cg_error_exit();
00282 CHECK ("ElectricFieldFlag", elecflag == 1);
00283 CHECK ("ElectricFieldModel", elecmodel == Voltage);
00284 CHECK ("MagneticFieldFlag", magnflag == 1);
00285 CHECK ("MagneticFieldModel", magnmodel == Interpolated);
00286 CHECK ("EMConductivityFlag", condflag == 1);
00287 CHECK ("EMConductivityModel", emconduct == Equilibrium_LinRessler);
00288
00289
00290
00291 puts ("checking family/rotating");
00292 if (cg_goto(cgfile, cgbase, "Family_t", 1, NULL) ||
00293 cg_rotating_read (rate, center))
00294 cg_error_exit();
00295 for (n = 0; n < 3; n++) {
00296 CHECK ("rotation rate", rate[n] == (float)n);
00297 CHECK ("rotation center", center[n] == (float)n);
00298 }
00299
00300
00301
00302 puts("checking FamilyBCDataSet");
00303 *name = 0;
00304 if (cg_goto(cgfile, cgbase, "Family_t", 1, "FamilyBC_t", 1, NULL) ||
00305 cg_bcdataset_info(&ndata) ||
00306 cg_bcdataset_read (1, name, &bctype, &dirichlet, &neumann))
00307 cg_error_exit();
00308 CHECK("bcdataset_info", ndata == 1);
00309 CHECK("bcdatset name", strcmp(name, "FamilyBCDataSet") == 0);
00310 CHECK("bcdatset type", bctype == BCWallInviscid);
00311 CHECK("bcdatset dirichlet", dirichlet == 1);
00312 CHECK("bcdatset neumann", neumann == 0);
00313
00314
00315
00316 puts("checking BC data");
00317 if (cg_boco_info (cgfile, cgbase, cgzone, 1, name, &bctype, &pttype,
00318 &n, size, &i, &dtype, &ndata))
00319 cg_error_exit();
00320 CHECK("BC_t name", strcmp(name, "BC") == 0);
00321 CHECK("BC_t type", bctype == BCWall);
00322 CHECK("BC_t pntset type", pttype == PointList);
00323 CHECK("BC_t npnts", n == 1);
00324
00325 if (cg_dataset_read (cgfile, cgbase, cgzone, 1, 1, name,
00326 &bctype, &dirichlet, &neumann) ||
00327 cg_goto (cgfile, cgbase, "Zone_t", 1, "ZoneBC_t", 1, "BC_t", 1,
00328 "BCDataSet_t", 1, NULL) ||
00329 cg_gridlocation_read (&gridloc) ||
00330 cg_ptset_info (&pttype, &n))
00331 cg_error_exit();
00332 CHECK("BCDataSet_t name", strcmp(name, "DataSet") == 0);
00333 CHECK("BCDataSet_t type", bctype == BCWallViscous);
00334 CHECK("BCDataSet_t location", gridloc == KFaceCenter);
00335 CHECK("BCDataSet_t pntset type", pttype == PointRange);
00336 CHECK("BC_t npnts", n == 2);
00337 CHECK("BCDataSet_t dirichlet", dirichlet == 1);
00338 CHECK("BCDataSet_t neumann", neumann == 0);
00339
00340
00341
00342 puts("checking user defined data");
00343 *name = 0;
00344 if (cg_goto (cgfile, cgbase, "UserDefinedData_t", 1, "end") ||
00345 cg_gridlocation_read (&gridloc) ||
00346 cg_famname_read (name) ||
00347 cg_ordinal_read (&ordinal) ||
00348 cg_narrays (&ndata))
00349 cg_error_exit ();
00350 CHECK ("gridlocation", gridloc == CellCenter);
00351 CHECK ("famname", strcmp (name, "Family") == 0);
00352 CHECK ("ordinal", ordinal == 0);
00353 CHECK ("narrays", ndata == 2);
00354
00355 if (cg_goto (cgfile, cgbase, "Zone_t", cgzone, "end") ||
00356 cg_nuser_data (&nuser))
00357 cg_error_exit ();
00358 CHECK ("nuserdata", nuser == 4);
00359
00360 for (i = 1; i <= 4; i++) {
00361 *name = 0;
00362 if (cg_goto (cgfile, cgbase, "Zone_t", cgzone,
00363 "UserDefinedData_t", i, "end") ||
00364 cg_gridlocation_read (&gridloc) ||
00365 cg_famname_read (name) ||
00366 cg_ordinal_read (&ordinal) ||
00367 cg_ptset_info (&pttype, &n) ||
00368 cg_ptset_read (ptlist) ||
00369 cg_narrays (&ndata) ||
00370 cg_nuser_data (&nuser))
00371 cg_error_exit ();
00372 CHECK ("gridlocation", gridloc == CellCenter);
00373 CHECK ("famname", strcmp (name, "Family") == 0);
00374 CHECK ("ordinal", ordinal == i);
00375 CHECK ("pointtype", pttype == PointList);
00376 CHECK ("npoints", n == 1);
00377 CHECK ("narrays", ndata == 2);
00378 CHECK ("nuserdata", nuser == 3);
00379
00380 for (j = 1; j <= 3; j++) {
00381 *name = 0;
00382 if (cg_goto (cgfile, cgbase, "Zone_t", cgzone,
00383 "UserDefinedData_t", i,
00384 "UserDefinedData_t", j, "end") ||
00385 cg_gridlocation_read (&gridloc) ||
00386 cg_famname_read (name) ||
00387 cg_ordinal_read (&ordinal) ||
00388 cg_ptset_info (&pttype, &n) ||
00389 cg_ptset_read (ptlist) ||
00390 cg_narrays (&ndata) ||
00391 cg_nuser_data (&nuser))
00392 cg_error_exit ();
00393 CHECK ("gridlocation", gridloc == Vertex);
00394 CHECK ("famname", strcmp (name, "Family") == 0);
00395 CHECK ("ordinal", ordinal == (i + j));
00396 CHECK ("pointtype", pttype == PointRange);
00397 CHECK ("npoints", n == 2);
00398 CHECK ("narrays", ndata == 2);
00399 CHECK ("nuserdata", nuser == 2);
00400
00401 for (n = 1; n <= 2; n++) {
00402 if (cg_goto (cgfile, cgbase, "Zone_t", cgzone,
00403 "UserDefinedData_t", i,
00404 "UserDefinedData_t", j,
00405 "DataArray_t", n, "end") ||
00406 cg_dataclass_read (&dclass) ||
00407 cg_nunits (&nunits) ||
00408 cg_unitsfull_read (&mass, &length, &time, &temp, &angle,
00409 ¤t, &amount, &intensity) ||
00410 cg_nexponents (&nexps) ||
00411 cg_expfull_read (exponents))
00412 cg_error_exit ();
00413 CHECK ("dataclass", dclass == Dimensional);
00414 CHECK ("nunits", nunits == 8);
00415 CHECK ("massunits", mass == Kilogram);
00416 CHECK ("lengthunits", length == Meter);
00417 CHECK ("timeunits", time == Second);
00418 CHECK ("tempunits", temp == Kelvin);
00419 CHECK ("angleunits", angle == Radian);
00420 CHECK ("currentunits", current == Ampere);
00421 CHECK ("amountunits", amount == Mole);
00422 CHECK ("intensityunits", intensity == Candela);
00423 CHECK ("nexponents", nexps == 8);
00424 for (n = 0; n < 8; n++)
00425 CHECK ("exponents", exponents[n] == (float)n);
00426 }
00427 }
00428 }
00429
00430 if (cg_goto (cgfile, cgbase, "Zone_t", cgzone,
00431 "UserDefinedData_t", 2, "UserDefinedData_t", 2,
00432 "UserDefinedData_t", 2, "UserDefinedData_t", 1, "end") ||
00433 cg_narrays (&ndata) ||
00434 cg_nuser_data (&nuser) ||
00435 cg_array_info (2, name, &dtype, &n, &dim) ||
00436 cg_array_read (1, &data1) ||
00437 cg_array_read (2, &data2))
00438 cg_error_exit ();
00439 CHECK ("narrays", ndata == 2);
00440 CHECK ("nuserdata", nuser == 0);
00441 CHECK ("arrayname", strcmp (name, "Data2") == 0);
00442 CHECK ("datatype", dtype == RealSingle);
00443 CHECK ("ndims", n == 1);
00444 CHECK ("dims", dim == 1);
00445 CHECK ("data1", data1 == 1.0);
00446 CHECK ("data2", data2 == 2.0);
00447
00448
00449
00450 puts("checking units and exponents");
00451 if (cg_goto(cgfile, cgbase, "UserDefinedData_t", 1,
00452 "DataArray_t", 1, NULL) ||
00453 cg_nunits (&nunits) ||
00454 cg_unitsfull_read (&mass, &length, &time, &temp, &angle,
00455 ¤t, &amount, &intensity) ||
00456 cg_nexponents (&nexps) ||
00457 cg_expfull_read (exponents))
00458 cg_error_exit ();
00459 CHECK ("nunits", nunits == 5);
00460 CHECK ("massunits", mass == Kilogram);
00461 CHECK ("lengthunits", length == Meter);
00462 CHECK ("timeunits", time == Second);
00463 CHECK ("tempunits", temp == Kelvin);
00464 CHECK ("angleunits", angle == Radian);
00465 CHECK ("currentunits", current == 0);
00466 CHECK ("amountunits", amount == 0);
00467 CHECK ("intensityunits", intensity == 0);
00468 CHECK ("nexponents", nexps == 5);
00469 for (n = 0; n < 5; n++)
00470 CHECK ("exponents", exponents[n] == (float)n);
00471 for (n = 6; n < 8; n++)
00472 CHECK ("exponents", exponents[n] == (float)0.0);
00473
00474
00475
00476 if (cg_goto(cgfile, cgbase, "Zone_t", 1, "UserDefinedData_t", 1,
00477 "DataArray_t", 1, NULL) ||
00478 cg_nunits (&nunits) ||
00479 cg_units_read (&mass, &length, &time, &temp, &angle) ||
00480 cg_nexponents (&nexps) ||
00481 cg_exponents_read (exponents))
00482 cg_error_exit ();
00483 CHECK ("nunits", nunits == 8);
00484 CHECK ("massunits", mass == Kilogram);
00485 CHECK ("lengthunits", length == Meter);
00486 CHECK ("timeunits", time == Second);
00487 CHECK ("tempunits", temp == Kelvin);
00488 CHECK ("angleunits", angle == Radian);
00489 CHECK ("nexponents", nexps == 8);
00490 for (n = 0; n < 5; n++)
00491 CHECK ("exponents", exponents[n] == (float)n);
00492
00493 puts ("closing file and reopening in modify mode");
00494 cg_close (cgfile);
00495
00496
00497
00498 if (cg_open (fname, CG_MODE_MODIFY, &cgfile))
00499 cg_error_exit ();
00500
00501 puts ("deleting user defined data and checking");
00502 if (cg_goto (cgfile, 1, "Zone_t", 1,
00503 "UserDefinedData_t", 3,
00504 "UserDefinedData_t", 2,
00505 "UserDefinedData_t", 1, "end") ||
00506 cg_nuser_data (&nuser))
00507 cg_error_exit ();
00508 CHECK ("nuserdata", nuser == 2);
00509 if (cg_delete_node ("User3.2.1.1") ||
00510 cg_nuser_data (&nuser))
00511 cg_error_exit ();
00512 CHECK ("nuserdata", nuser == 1);
00513
00514
00515
00516 cg_configure(CG_CONFIG_COMPRESS, (void *)0);
00517
00518 puts ("closing file");
00519 cg_close (cgfile);
00520
00521 return 0;
00522 }
00523