00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <string.h>
00013 #ifdef _WIN32
00014 # include <io.h>
00015 #else
00016 # include <unistd.h>
00017 #endif
00018 #include "getargs.h"
00019 #include "cgnslib.h"
00020 #include "cgns_header.h"
00021 #include "cgns_io.h"
00022
00023 #if !defined(CGNS_VERSION) || CGNS_VERSION < 3000
00024 # error You need at least CGNS Version 3.0
00025 #endif
00026
00027 static cgns_base *CurrentBase;
00028 static cgns_zone *CurrentZone;
00029
00030 static char *inpfile = NULL;
00031 static char *outfile = NULL;
00032
00033 static cgns_file *cgfile;
00034
00035 static int inpcgio, outcgio;
00036 static double inproot = -1.0;
00037 static double outroot = -1.0;
00038
00039 static float FloatVersion;
00040 static int LibraryVersion = CGNS_VERSION;
00041 static int FileVersion;
00042 static int FromVersion;
00043
00044 static int VersionList[] = {
00045 1200, 1270, 2000, 2100, 2200, 2300, 2400, 2420, 2460,
00046 2500, 2510, 2520, 2530, 3000
00047 };
00048 #define nVersions (sizeof(VersionList)/sizeof(int))
00049
00050 static char datatype[CGIO_MAX_NAME_LENGTH+1];
00051 static char label[CGIO_MAX_NAME_LENGTH+1];
00052 static char linkfile[CGIO_MAX_FILE_LENGTH+1];
00053 static char linkpath[CGIO_MAX_LINK_LENGTH+1];
00054
00055 static int numdims, dimvals[CGIO_MAX_DIMENSIONS];
00056
00057
00058
00059 static int verbose = 0;
00060 static int keep_links = 1;
00061 static int keep_nodes = 0;
00062
00063 static char options[] = "vrk";
00064
00065 static char *usgmsg[] = {
00066 "usage : cgnsversion [options] Version CGNSfile [CGNSoutfile]",
00067 "options:",
00068 " -v : verbose output",
00069 " -r : remove links",
00070 " -k : keep all nodes",
00071 NULL
00072 };
00073
00074
00075
00076
00077 static void error_exit (char *msg, int errcode)
00078 {
00079 if (errcode > 0) {
00080 char errmsg[128];
00081 cgio_error_message (sizeof(errmsg), errmsg);
00082 fprintf (stderr, "%s:%s\n", msg, errmsg);
00083 }
00084 else if (errcode < 0)
00085 fprintf (stderr, "%s:%s\n", msg, cg_get_error());
00086 else
00087 fprintf (stderr, "%s\n", msg);
00088
00089
00090
00091 cgio_cleanup ();
00092 if (outfile != NULL) unlink (outfile);
00093 exit (1);
00094 }
00095
00096
00097
00098 static size_t get_size (char *type, int nd, int *dims)
00099 {
00100 int n;
00101 size_t size = 1;
00102
00103 if (nd < 1) return 0;
00104 for (n = 0; n < nd; n++)
00105 size *= (size_t)dims[n];
00106 switch (*type) {
00107 case 'B':
00108 case 'C':
00109 if (type[1] == '1') return size;
00110 break;
00111 case 'I':
00112 case 'U':
00113 if (type[1] == '4') return size * sizeof(int);
00114 if (type[1] == '8') return size * sizeof(long);
00115 break;
00116 case 'R':
00117 if (type[1] == '4') return size * sizeof(float);
00118 if (type[1] == '8') return size * sizeof(double);
00119 break;
00120 case 'X':
00121 if (type[1] == '4') return 2 * size * sizeof(float);
00122 if (type[1] == '8') return 2 * size * sizeof(double);
00123 break;
00124 }
00125 return 0;
00126 }
00127
00128
00129
00130 static void copy_1200 (double inpid, double outid)
00131 {
00132 int size;
00133 void *data;
00134
00135 if (cgio_get_label (inpcgio, inpid, label))
00136 error_exit ("cgio_get_label", 1);
00137 if (cgio_set_label (outcgio, outid, label))
00138 error_exit ("cgio_set_label", 1);
00139
00140 if (cgio_get_data_type (inpcgio, inpid, datatype))
00141 error_exit ("cgio_get_data_type", 1);
00142 if (cgio_get_dimensions (inpcgio, inpid, &numdims, dimvals))
00143 error_exit ("cgio_get_dimensions", 1);
00144 if (numdims == 0) return;
00145 if (FromVersion == 1200 && numdims == 1) {
00146 numdims = 2;
00147 dimvals[1] = dimvals[0];
00148 dimvals[0] = 1;
00149 }
00150 if (FileVersion == 1200 && numdims == 2) {
00151 numdims = 1;
00152 dimvals[0] = dimvals[1];
00153 }
00154 if (cgio_set_dimensions (outcgio, outid, datatype, numdims, dimvals))
00155 error_exit ("cgio_set_dimensions", 1);
00156
00157 size = get_size (datatype, numdims, dimvals);
00158 if (size == 0) return;
00159 if ((data = malloc (size)) == NULL)
00160 error_exit ("malloc failed for data", 0);
00161 if (cgio_read_all_data (inpcgio, inpid, data))
00162 error_exit ("cgio_read_all_data", 1);
00163 if (cgio_write_all_data (outcgio, outid, data))
00164 error_exit ("cgio_write_all_data", 1);
00165 free (data);
00166 }
00167
00168
00169
00170 static double get_child_id (int cgio, double parid, int nchild)
00171 {
00172 int cnt;
00173 double childid;
00174
00175 if (cgio_children_ids (cgio, parid, nchild, 1, &cnt, &childid))
00176 error_exit ("cgio_children_ids", 1);
00177 return childid;
00178 }
00179
00180
00181
00182 static double create_node (double inpid, double parid, int recurse)
00183 {
00184 int nchild, nc, len_ret;
00185 char name[CGIO_MAX_NAME_LENGTH+1],type[CGIO_MAX_NAME_LENGTH+1];
00186 double outid, inpchild;
00187
00188 if (cgio_get_name (inpcgio, inpid, name))
00189 error_exit ("cgio_get_name", 1);
00190 if (cgio_get_label (inpcgio, inpid, type))
00191 error_exit ("cgio_get_label", 1);
00192
00193 if (!keep_nodes) {
00194 if (FileVersion < 2100 &&
00195 0 == strcmp (type, "UserDefinedData_t"))
00196 return 0.0;
00197 if (FileVersion < 2400 &&
00198 (0 == strcmp (type, "AdditionalUnits_t") ||
00199 0 == strcmp (type, "AdditionalExponents_t")))
00200 return 0.0;
00201 }
00202
00203 if (cgio_is_link (inpcgio, inpid, &len_ret))
00204 error_exit ("cgio_is_link", 1);
00205
00206
00207
00208 if (len_ret > 0 && keep_links) {
00209 if (cgio_get_link (inpcgio, inpid, linkfile, linkpath))
00210 error_exit ("cgio_get_link", 1);
00211 if (cgio_create_link (outcgio, parid, name, linkfile,
00212 linkpath, &outid))
00213 error_exit ("cgio_create_link", 1);
00214 return 0.0;
00215 }
00216
00217
00218
00219 if (cgio_create_node (outcgio, parid, name, &outid))
00220 error_exit ("cgio_create_node", 1);
00221
00222
00223
00224 if (cgio_copy_node (inpcgio, inpid, outcgio, outid))
00225 error_exit ("cgio_copy_node", 1);
00226
00227 if (!recurse) return outid;
00228
00229
00230
00231 if (cgio_number_children (inpcgio, inpid, &nchild))
00232 error_exit ("cgio_number_children", 1);
00233 if (nchild < 1) return outid;
00234
00235 if (FileVersion < 2100 &&
00236 0 == strcmp (type, "FlowEquationSet_t")) {
00237 for (nc = 1; nc <= nchild; nc++) {
00238 inpchild = get_child_id (inpcgio, inpid, nc);
00239 if (cgio_get_label (inpcgio, inpchild, label))
00240 error_exit ("cgio_get_label", 1);
00241 if (strcmp (label, "ThermalRelaxationModel_t") &&
00242 strcmp (label, "ChemicalKineticsModel_t") &&
00243 strcmp (label, "EMElectricFieldModel_t") &&
00244 strcmp (label, "EMMagneticFieldModel_t") &&
00245 strcmp (label, "EMConductivityModel_t"))
00246 create_node (inpchild, outid, 1);
00247 }
00248 }
00249 else if (FileVersion < 2400 &&
00250 0 == strcmp (type, "FlowEquationSet_t")) {
00251 for (nc = 1; nc <= nchild; nc++) {
00252 inpchild = get_child_id (inpcgio, inpid, nc);
00253 if (cgio_get_label (inpcgio, inpchild, label))
00254 error_exit ("cgio_get_label", 1);
00255 if (strcmp (label, "EMElectricFieldModel_t") &&
00256 strcmp (label, "EMMagneticFieldModel_t") &&
00257 strcmp (label, "EMConductivityModel_t"))
00258 create_node (inpchild, outid, 1);
00259 }
00260 }
00261 else if (FileVersion < 2400 &&
00262 0 == strcmp (type, "UserDefinedData_t")) {
00263 for (nc = 1; nc <= nchild; nc++) {
00264 inpchild = get_child_id (inpcgio, inpid, nc);
00265 if (cgio_get_label (inpcgio, inpchild, label))
00266 error_exit ("cgio_get_label", 1);
00267 if (strcmp (label, "UserDefinedData_t") &&
00268 strcmp (label, "FamilyName_t") &&
00269 strcmp (label, "Ordinal_t") &&
00270 strcmp (label, "GridLocation_t") &&
00271 strcmp (label, "IndexArray_t") &&
00272 strcmp (label, "IndexRange_t"))
00273 create_node (inpchild, outid, 1);
00274 }
00275 }
00276 else if (FileVersion < 2400 &&
00277 0 == strcmp (type, "GridConnectivity1to1_t")) {
00278 for (nc = 1; nc <= nchild; nc++) {
00279 inpchild = get_child_id (inpcgio, inpid, nc);
00280 if (cgio_get_label (inpcgio, inpchild, label))
00281 error_exit ("cgio_get_label", 1);
00282 if (strcmp (label, "GridConnectivityProperty_t"))
00283 create_node (inpchild, outid, 1);
00284 }
00285 }
00286 else if (FileVersion < 2400 &&
00287 0 == strcmp (type, "Family_t")) {
00288 for (nc = 1; nc <= nchild; nc++) {
00289 inpchild = get_child_id (inpcgio, inpid, nc);
00290 if (cgio_get_label (inpcgio, inpchild, label))
00291 error_exit ("cgio_get_label", 1);
00292 if (strcmp (label, "RotatingCoordinates_t"))
00293 create_node (inpchild, outid, 1);
00294 }
00295 }
00296 else if (FileVersion < 2400 &&
00297 0 == strcmp (type, "FamilyBC_t")) {
00298 for (nc = 1; nc <= nchild; nc++) {
00299 inpchild = get_child_id (inpcgio, inpid, nc);
00300 if (cgio_get_label (inpcgio, inpchild, label))
00301 error_exit ("cgio_get_label", 1);
00302 if (strcmp (label, "BCDataSet_t"))
00303 create_node (inpchild, outid, 1);
00304 }
00305 }
00306 else {
00307 for (nc = 1; nc <= nchild; nc++) {
00308 inpchild = get_child_id (inpcgio, inpid, nc);
00309 create_node (inpchild, outid, 1);
00310 }
00311 }
00312 return outid;
00313 }
00314
00315
00316
00317 void fix_name (int cgio, double pid, double id, char *newname)
00318 {
00319 char oldname[CGIO_MAX_NAME_LENGTH+1];
00320
00321 if (cgio_get_name (cgio, id, oldname))
00322 error_exit ("cgio_get_name", 1);
00323 if (strcmp (oldname, newname) &&
00324 cgio_set_name (cgio, pid, id, newname))
00325 error_exit ("cgio_set_name", 1);
00326 }
00327
00328
00329
00330 void CGI_write_dataset(double parent_id, cgns_dataset *dataset,
00331 GridLocation_t location) {
00332 int dim_vals, n;
00333 const char *type_name;
00334
00335 if (dataset->link && keep_links) {
00336 create_node (dataset->id, parent_id, 0);
00337 return;
00338 }
00339
00340
00341 type_name = cg_BCTypeName(dataset->type);
00342 dim_vals= strlen(type_name);
00343 if (cgio_new_node (outcgio, parent_id, dataset->name, "BCDataSet_t",
00344 "C1", 1, &dim_vals, type_name, &dataset->id))
00345 error_exit ("cgio_new_node", 1);
00346
00347
00348 if (FileVersion >= 2400) location = dataset->location;
00349 if (location != Vertex && (FileVersion == 1200 || FileVersion >= 2400)) {
00350 double dummy_id;
00351 type_name = cg_GridLocationName(location);
00352 dim_vals = strlen(type_name);
00353 if (cgio_new_node (outcgio, dataset->id, "GridLocation",
00354 "GridLocation_t", "C1", 1, &dim_vals,
00355 type_name, &dummy_id))
00356 error_exit ("cgio_new_node", 1);
00357 }
00358
00359
00360 if (dataset->dirichlet)
00361 create_node (dataset->dirichlet->id, dataset->id, 1);
00362
00363
00364 if (dataset->neumann)
00365 create_node (dataset->neumann->id, dataset->id, 1);
00366
00367
00368 for (n=0; n<dataset->ndescr; n++)
00369 create_node (dataset->descr[n].id, dataset->id, 1);
00370
00371
00372 if (dataset->state) create_node (dataset->state->id, dataset->id, 1);
00373
00374
00375 if (dataset->data_class &&
00376 cgi_write_dataclass (dataset->id, dataset->data_class))
00377 error_exit ("cgi_write_dataclass", -1);
00378
00379
00380 if (dataset->units) create_node (dataset->units->id, dataset->id, 1);
00381
00382 if (keep_nodes || FileVersion >= 2100) {
00383
00384 for (n=0; n<dataset->nuser_data; n++)
00385 create_node (dataset->user_data[n].id, dataset->id, 1);
00386 }
00387 if (dataset->ptset && (keep_nodes || FileVersion >= 2400))
00388 create_node (dataset->ptset->id, dataset->id, 1);
00389 }
00390
00391
00392
00393 void CGI_write_boco (double parent_id, cgns_boco *boco) {
00394 int dim_vals, n;
00395 double dummy_id;
00396 const char *type_name;
00397 BCType_t type;
00398 GridLocation_t location = boco->location;
00399
00400 if (boco->link && keep_links) {
00401 create_node (boco->id, parent_id, 0);
00402 return;
00403 }
00404
00405
00406 type = boco->type;
00407 if (type == FamilySpecified && FileVersion < 2000) {
00408 printf ("WARNING:BC type FamilySpecified changed to UserDefined\n");
00409 printf (" for boundary condition \"%s\"\n", boco->name);
00410 type = BCTypeUserDefined;
00411 }
00412 type_name = cg_BCTypeName(type);
00413 dim_vals = strlen(type_name);
00414 if (cgio_new_node (outcgio, parent_id, boco->name, "BC_t",
00415 "C1", 1, &dim_vals, type_name, &boco->id))
00416 error_exit ("cgio_new_node", 1);
00417
00418 if (boco->ptset) {
00419 PointSetType_t ptype = boco->ptset->type;
00420
00421
00422
00423 if (ptype == PointList || ptype == PointRange) {
00424 if (location != Vertex) {
00425 if (FileVersion == 1200 || FileVersion >= 2300) {
00426 if (ptype == PointList)
00427 ptype = ElementList;
00428 else
00429 ptype = ElementRange;
00430 location = Vertex;
00431 }
00432 }
00433 }
00434 else if (ptype == ElementList || ptype == ElementRange) {
00435 if (FileVersion > 1200 && FileVersion < 2300) {
00436 if (ptype == ElementList)
00437 ptype = PointList;
00438 else
00439 ptype = PointRange;
00440 location = FaceCenter;
00441 }
00442 else
00443 location = Vertex;
00444 }
00445 else
00446 ptype = 0;
00447
00448 if (ptype) {
00449 type_name = cg_PointSetTypeName(ptype);
00450 if (cgio_create_node (outcgio, boco->id, type_name, &dummy_id))
00451 error_exit ("cgio_create_node", 1);
00452 if (CurrentZone->type == Unstructured &&
00453 ((FileVersion == 1200 &&
00454 (ptype == ElementList ||
00455 ptype == ElementRange)) ||
00456 (FromVersion == 1200 &&
00457 (boco->ptset->type == ElementList ||
00458 boco->ptset->type == ElementRange))))
00459 copy_1200 (boco->ptset->id, dummy_id);
00460 else {
00461 if (cgio_copy_node (inpcgio, boco->ptset->id,
00462 outcgio, dummy_id))
00463 error_exit ("cgio_copy_node", 1);
00464 }
00465 }
00466 }
00467
00468
00469 if (location != Vertex && FileVersion > 1200) {
00470 type_name = cg_GridLocationName(location);
00471 dim_vals = strlen(type_name);
00472 if (cgio_new_node (outcgio, boco->id, "GridLocation",
00473 "GridLocation_t", "C1", 1, &dim_vals, type_name, &dummy_id))
00474 error_exit ("cgio_new_node", 1);
00475 }
00476
00477
00478 if (boco->family_name[0]!='\0') {
00479 if (FileVersion == 1200) {
00480 if (cgio_new_node (outcgio, boco->id, boco->family_name,
00481 "FamilyName_t", "MT",0, &dim_vals, NULL, &dummy_id))
00482 error_exit ("cgio_new_node", 1);
00483 }
00484 else {
00485 dim_vals = strlen(boco->family_name);
00486 if (cgio_new_node (outcgio, boco->id, "FamilyName",
00487 "FamilyName_t", "C1",1, &dim_vals,
00488 boco->family_name, &dummy_id))
00489 error_exit ("cgio_new_node", 1);
00490 }
00491 }
00492
00493
00494 for (n=0; n<boco->ndataset; n++)
00495 CGI_write_dataset (boco->id, &boco->dataset[n], boco->location);
00496
00497
00498 if (boco->Nindex) {
00499 if (cgio_new_node (outcgio, boco->id, "InwardNormalIndex",
00500 "\"int[IndexDimension]\"", "I4", 1,
00501 &(CurrentZone->index_dim), boco->Nindex, &boco->index_id))
00502 error_exit ("cgio_new_node", 1);
00503 }
00504
00505
00506 if (boco->normal) create_node (boco->normal->id, boco->id, 0);
00507
00508
00509 for (n=0; n<boco->ndescr; n++)
00510 create_node (boco->descr[n].id, boco->id, 1);
00511
00512
00513 if (boco->state) create_node (boco->state->id, boco->id, 1);
00514
00515
00516 if (boco->data_class &&
00517 cgi_write_dataclass(boco->id, boco->data_class))
00518 error_exit (NULL, 0);
00519
00520
00521 if (boco->units) create_node (boco->units->id, boco->id, 1);
00522
00523
00524 if (boco->ordinal && cgi_write_ordinal(boco->id, boco->ordinal))
00525 error_exit ("cgi_write_ordinal", -1);
00526
00527 if (keep_nodes || FileVersion >= 2100) {
00528
00529 if (boco->bprop && (keep_nodes || FileVersion >= 2200))
00530 create_node (boco->bprop->id, boco->id, 1);
00531
00532 for (n=0; n<boco->nuser_data; n++)
00533 create_node (boco->user_data[n].id, boco->id, 1);
00534 }
00535 }
00536
00537
00538
00539 void CGI_write_zboco(double parent_id, cgns_zboco *zboco) {
00540 int n;
00541
00542 if (zboco->link && keep_links) {
00543 create_node (zboco->id, parent_id, 0);
00544 return;
00545 }
00546
00547
00548 if (cgio_new_node (outcgio, parent_id, "ZoneBC", "ZoneBC_t",
00549 "MT", 0, 0, 0, &zboco->id))
00550 error_exit ("cgio_new_node", 1);
00551
00552
00553 for (n=0; n<zboco->nbocos; n++)
00554 CGI_write_boco (zboco->id, &zboco->boco[n]);
00555
00556
00557 for (n=0; n<zboco->ndescr; n++)
00558 create_node (zboco->descr[n].id, zboco->id, 1);
00559
00560
00561 if (zboco->state) create_node (zboco->state->id, zboco->id, 1);
00562
00563
00564 if (zboco->data_class &&
00565 cgi_write_dataclass (zboco->id, zboco->data_class))
00566 error_exit (NULL, 0);
00567
00568
00569 if (zboco->units) create_node (zboco->units->id, zboco->id, 1);
00570
00571 if (keep_nodes || FileVersion >= 2100) {
00572
00573 for (n=0; n<zboco->nuser_data; n++)
00574 create_node (zboco->user_data[n].id, zboco->id, 1);
00575 }
00576 }
00577
00578
00579
00580 void CGI_write_conns (double parent_id, cgns_conn *conn) {
00581 int n, dim_vals;
00582 double dummy_id;
00583 const char *type_name;
00584 cgns_ptset *ptset;
00585 GridLocation_t location = conn->location;
00586
00587 if (conn->link && keep_links) {
00588 create_node (conn->id, parent_id, 0);
00589 return;
00590 }
00591
00592 dim_vals = strlen(conn->donor);
00593 if (cgio_new_node (outcgio, parent_id, conn->name, "GridConnectivity_t",
00594 "C1", 1, &dim_vals, conn->donor, &conn->id))
00595 error_exit ("cgio_new_node", 1);
00596
00597
00598 type_name = cg_GridConnectivityTypeName(conn->type);
00599 dim_vals = strlen(type_name);
00600 if (cgio_new_node (outcgio, conn->id, "GridConnectivityType",
00601 "GridConnectivityType_t", "C1", 1, &dim_vals,
00602 type_name, &dummy_id))
00603 error_exit ("cgio_new_node", 1);
00604
00605
00606 if (location != Vertex) {
00607 if (location != CellCenter && FileVersion < 2300) {
00608 if (FileVersion == 2200)
00609 location = FaceCenter;
00610 else
00611 location = CellCenter;
00612 }
00613 type_name = cg_GridLocationName(location);
00614 dim_vals = strlen(type_name);
00615 if (cgio_new_node (outcgio, conn->id, "GridLocation",
00616 "GridLocation_t", "C1", 1, &dim_vals,
00617 type_name, &dummy_id))
00618 error_exit ("cgio_new_node", 1);
00619 }
00620 if (location != conn->location) {
00621 printf ("WARNING:GridLocation changed from %s to %s\n",
00622 cg_GridLocationName(conn->location), type_name);
00623 printf (" for connectivity \"%s\"\n", conn->name);
00624 }
00625
00626
00627
00628 ptset = &(conn->ptset);
00629 create_node (ptset->id, conn->id, 0);
00630
00631
00632 ptset = &(conn->dptset);
00633 if (FileVersion > 1200) {
00634 create_node (ptset->id, conn->id, 0);
00635 if (conn->interpolants)
00636 create_node (conn->interpolants->id, conn->id, 1);
00637 }
00638 else {
00639 double donorid;
00640 ZoneType_t dtype = ZoneTypeNull;
00641 PointSetType_t ptype = ptset->type;
00642 for (n = 0; n < CurrentBase->nzones; n++) {
00643 if (0 == strcmp (conn->donor, CurrentBase->zone[n].name)) {
00644 dtype = CurrentBase->zone[n].type;
00645 break;
00646 }
00647 }
00648 if (dtype == Structured) {
00649 if (cgio_new_node (outcgio, conn->id, "StructuredDonor",
00650 "StructuredDonor_t", "MT", 0,
00651 &dim_vals, NULL, &dummy_id))
00652 error_exit ("cgio_new_node", 1);
00653 donorid = create_node (ptset->id, dummy_id, 0);
00654 ptype = PointListDonor;
00655 }
00656 else if (dtype == Unstructured) {
00657 if (cgio_new_node (outcgio, conn->id, "UnstructuredDonor",
00658 "UnstructuredDonor_t", "MT", 0,
00659 &dim_vals, NULL, &dummy_id))
00660 error_exit ("cgio_new_node", 1);
00661 donorid = create_node (ptset->id, dummy_id, 0);
00662 if (ptype != PointListDonor && ptype != CellListDonor)
00663 ptype = (location == Vertex || conn->interpolants == NULL) ?
00664 PointListDonor : CellListDonor;
00665 if (ptype == CellListDonor && conn->interpolants)
00666 create_node (conn->interpolants->id, dummy_id, 0);
00667 }
00668 else {
00669 printf ("ERROR:couldn't find donor zone \"%s\" for \"%s\"\n",
00670 conn->donor, conn->name);
00671 printf (" therefore donor data was not written\n");
00672 donorid = 0.0;
00673 }
00674 if (ptype != ptset->type && donorid > 0.0) {
00675 type_name = cg_PointSetTypeName(ptype);
00676 fix_name (outcgio, dummy_id, donorid, (char *)type_name);
00677 printf ("WARNING:%s changed to %s\n",
00678 cg_PointSetTypeName(ptset->type), type_name);
00679 printf (" for connectivity \"%s\"\n", conn->name);
00680 }
00681 }
00682
00683
00684 for (n=0; n<conn->ndescr; n++)
00685 create_node (conn->descr[n].id, conn->id, 1);
00686
00687
00688 if (conn->ordinal && cgi_write_ordinal(conn->id, conn->ordinal))
00689 cg_error_exit();
00690
00691 if (keep_nodes || FileVersion >= 2100) {
00692
00693 if (conn->cprop && (keep_nodes || FileVersion >= 2200))
00694 create_node (conn->cprop->id, conn->id, 1);
00695
00696 for (n=0; n<conn->nuser_data; n++)
00697 create_node (conn->user_data[n].id, conn->id, 1);
00698 }
00699 }
00700
00701
00702
00703 void CGI_write_zconn(double parent_id, cgns_zconn *zconn) {
00704 int n;
00705
00706 if (zconn->link && keep_links) {
00707 create_node (zconn->id, parent_id, 0);
00708 return;
00709 }
00710
00711
00712 if (cgio_new_node (outcgio, parent_id, "ZoneGridConnectivity",
00713 "ZoneGridConnectivity_t", "MT", 0, 0, 0, &zconn->id))
00714 error_exit ("cgio_new_node", 1);
00715
00716
00717 for (n=0; n<zconn->n1to1; n++)
00718 create_node (zconn->one21[n].id, zconn->id, 1);
00719
00720
00721 for (n=0; n<zconn->nconns; n++)
00722 CGI_write_conns (zconn->id, &zconn->conn[n]);
00723
00724
00725 for (n=0; n<zconn->nholes; n++)
00726 create_node (zconn->hole[n].id, zconn->id, 1);
00727
00728
00729 for (n=0; n<zconn->ndescr; n++)
00730 create_node (zconn->descr[n].id, zconn->id, 1);
00731
00732 if (keep_nodes || FileVersion >= 2100) {
00733
00734 for (n=0; n<zconn->nuser_data; n++)
00735 create_node (zconn->user_data[n].id, zconn->id, 1);
00736 }
00737 }
00738
00739
00740
00741 void CGI_write_section(double parent_id, cgns_section *section) {
00742 int n, dim_vals, data[2];
00743 double dummy_id;
00744
00745 if ((section->link && keep_links) || section->el_type <= PYRA_5 ||
00746 (FromVersion >= 3000 && FileVersion >= 3000)) {
00747 create_node (section->id, parent_id, 1);
00748 return;
00749 }
00750
00751 if (FileVersion < 3000) {
00752 int i, j, ne, nelems, *elems;
00753
00754 if (section->el_type == NFACE_n) {
00755 printf ("can't convert NFACE_n element set - skipping\n");
00756 return;
00757 }
00758 nelems = section->range[1] - section->range[0] + 1;
00759 elems = (int *)section->connect->data;
00760
00761 if (section->el_type == PYRA_13) {
00762 i = 5;
00763 j = 13;
00764 for (ne = 1; ne < nelems; ne++) {
00765 for (n = 0; n < 5; n++)
00766 elems[i++] = elems[j++];
00767 j += 8;
00768 }
00769 section->connect->dim_vals[0] = nelems * 5;
00770 printf("converted %d PYRA_13 elements to PYRA_5\n", nelems);
00771 }
00772
00773 if (section->el_type == MIXED) {
00774 ElementType_t type;
00775 int npe, cnt = 0;
00776
00777 i = j = 0;
00778 for (ne = 0; ne < nelems; ne++) {
00779 type = elems[j++];
00780 if (type > NGON_n)
00781 npe = type - NGON_n;
00782 else
00783 cg_npe (type, &npe);
00784 if (npe <= 0)
00785 error_exit("invalid element found in MIXED elements", 0);
00786 if (type > PYRA_5) {
00787 elems[i++] = (int)(type - 1);
00788 if (type == PYRA_13) npe = 5;
00789 }
00790 else
00791 elems[i++] = type;
00792 if (j > i) {
00793 for (n = 0; n < npe; n++)
00794 elems[i+n] = elems[j+n];
00795 }
00796 i += npe;
00797 j += npe;
00798 if (type == PYRA_13) {
00799 cnt++;
00800 j += 8;
00801 }
00802 }
00803 if (cnt) {
00804 printf("converted %d PYRA_13 elements to PYRA_5 in MIXED\n", cnt);
00805 section->connect->dim_vals[0] -= 8 * cnt;
00806 }
00807 }
00808 (section->el_type)--;
00809 }
00810
00811 dim_vals = 2;
00812 data[0] = section->el_type;
00813 data[1] = section->el_bound;
00814 if (cgio_new_node (outcgio, parent_id, section->name, "Elements_t",
00815 "I4", 1, &dim_vals, data, §ion->id))
00816 error_exit ("cgio_new_node", 1);
00817
00818
00819 if (cgio_new_node (outcgio, section->id, "ElementRange", "IndexRange_t",
00820 "I4", 1, &dim_vals, section->range, &dummy_id))
00821 error_exit ("cgio_new_node", 1);
00822
00823
00824 if (cgi_write_array(section->id, section->connect))
00825 cg_error_exit();
00826
00827
00828 if (section->parent && cgi_write_array(section->id, section->parent))
00829 cg_error_exit();
00830
00831
00832 for (n=0; n<section->ndescr; n++)
00833 create_node (section->descr[n].id, section->id, 1);
00834
00835 if (keep_nodes || FileVersion >= 2100) {
00836
00837 for (n=0; n<section->nuser_data; n++)
00838 create_node (section->user_data[n].id, section->id, 1);
00839 }
00840 }
00841
00842
00843
00844 void CGI_write_zone(double parent_id, cgns_zone *zone) {
00845 int n, dim_vals[2];
00846 double dummy_id;
00847 const char *type_name;
00848
00849 CurrentZone = zone;
00850 if (zone->link && keep_links) {
00851 create_node (zone->id, parent_id, 0);
00852 return;
00853 }
00854
00855
00856 dim_vals[0]= zone->index_dim;
00857 dim_vals[1]= 3;
00858 if (cgio_new_node (outcgio, parent_id, zone->name, "Zone_t",
00859 "I4", 2, dim_vals, zone->nijk, &zone->id))
00860 error_exit ("cgio_new_node", 1);
00861
00862
00863 type_name = cg_ZoneTypeName(zone->type);
00864 dim_vals[0] = strlen(type_name);
00865 if (cgio_new_node (outcgio, zone->id, "ZoneType", "ZoneType_t",
00866 "C1", 1, dim_vals, type_name, &dummy_id))
00867 error_exit ("cgio_new_node", 1);
00868
00869
00870 if (zone->nzcoor >= 1) {
00871 if (FileVersion >= 2000) {
00872 for (n=0; n<zone->nzcoor; n++)
00873 create_node (zone->zcoor[n].id, zone->id, 1);
00874 }
00875 else {
00876 dummy_id = create_node (zone->zcoor->id, zone->id, 1);
00877 fix_name (outcgio, zone->id, dummy_id, "GridCoordinates");
00878 }
00879 }
00880
00881
00882 if (zone->family_name[0]!='\0') {
00883 if (FileVersion == 1200) {
00884 if (cgio_new_node (outcgio, zone->id, zone->family_name,
00885 "FamilyName_t", "MT",0, dim_vals, NULL, &dummy_id))
00886 error_exit ("cgio_new_node", 1);
00887 }
00888 else {
00889 dim_vals[0] = strlen(zone->family_name);
00890 if (cgio_new_node (outcgio, zone->id, "FamilyName",
00891 "FamilyName_t", "C1",1, dim_vals,
00892 zone->family_name, &dummy_id))
00893 error_exit ("cgio_new_node", 1);
00894 }
00895 }
00896
00897
00898 for (n=0; n<zone->nsections; n++)
00899 CGI_write_section (zone->id, &zone->section[n]);
00900
00901
00902 for (n=0; n<zone->nsols; n++)
00903 create_node (zone->sol[n].id, zone->id, 1);
00904
00905
00906 if (zone->zconn) CGI_write_zconn(zone->id, zone->zconn);
00907
00908
00909 if (zone->zboco) CGI_write_zboco(zone->id, zone->zboco);
00910
00911
00912 for (n=0; n<zone->ndiscrete; n++)
00913 create_node (zone->discrete[n].id, zone->id, 1);
00914
00915
00916 for (n=0; n<zone->ndescr; n++)
00917 create_node (zone->descr[n].id, zone->id, 1);
00918
00919
00920 if (zone->state) create_node (zone->state->id, zone->id, 1);
00921
00922
00923 if (zone->data_class && cgi_write_dataclass (zone->id, zone->data_class))
00924 error_exit ("cgi_write_dataclass", -1);
00925
00926
00927 if (zone->units) create_node (zone->units->id, zone->id, 1);
00928
00929
00930 if (zone->converg) create_node (zone->converg->id, zone->id, 1);
00931
00932
00933 if (zone->equations) create_node (zone->equations->id, zone->id, 1);
00934
00935
00936 for (n=0; n<zone->nintegrals; n++)
00937 create_node (zone->integral[n].id, zone->id, 1);
00938
00939
00940 if (zone->ordinal && cgi_write_ordinal (zone->id, zone->ordinal))
00941 cg_error_exit();
00942
00943 if (keep_nodes || FileVersion >= 2000) {
00944
00945 for (n=0; n<zone->nrmotions; n++)
00946 create_node (zone->rmotion[n].id, zone->id, 1);
00947
00948
00949 for (n=0; n<zone->namotions; n++)
00950 create_node (zone->amotion[n].id, zone->id, 1);
00951
00952
00953 if (zone->ziter) create_node (zone->ziter->id, zone->id, 1);
00954
00955 if (keep_nodes || FileVersion >= 2100) {
00956
00957 for (n=0; n<zone->nuser_data; n++)
00958 create_node (zone->user_data[n].id, zone->id, 1);
00959
00960 if (zone->rotating && (keep_nodes || FileVersion >= 2200))
00961 create_node (zone->rotating->id, zone->id, 1);
00962 }
00963 }
00964 }
00965
00966
00967
00968 void CGI_write () {
00969 cgns_base *base;
00970 int n, b;
00971 int dim_vals, data[2];
00972 double dummy_id;
00973
00974
00975 dim_vals = 1;
00976 if (cgio_new_node (outcgio, cgfile->rootid, "CGNSLibraryVersion",
00977 "CGNSLibraryVersion_t", "R4", 1, &dim_vals,
00978 &FloatVersion, &dummy_id))
00979 error_exit ("cgio_new_node", 1);
00980
00981
00982 for (b=0; b < cgfile->nbases; b++) {
00983 CurrentBase = base = &(cgfile->base[b]);
00984
00985 data[0]=base->cell_dim;
00986 data[1]=base->phys_dim;
00987
00988
00989 dim_vals=2;
00990 if (cgio_new_node (outcgio, cgfile->rootid, base->name, "CGNSBase_t",
00991 "I4", 1, &dim_vals, data, &base->id))
00992 error_exit ("cgio_new_node", 1);
00993
00994
00995 for (n=0; n<base->ndescr; n++)
00996 create_node (base->descr[n].id, base->id, 1);
00997
00998
00999 if (base->state) create_node (base->state->id, base->id, 1);
01000
01001 if (keep_nodes || FileVersion >= 2200) {
01002
01003 if (base->gravity) create_node (base->gravity->id, base->id, 1);
01004
01005
01006 if (base->axisym) create_node (base->axisym->id, base->id, 1);
01007
01008
01009 if (base->rotating) create_node (base->rotating->id, base->id, 1);
01010 }
01011
01012
01013 for (n=0; n<base->nzones; n++)
01014 CGI_write_zone(base->id, &base->zone[n]);
01015
01016
01017 for (n=0; n<base->nfamilies; n++)
01018 create_node (base->family[n].id, base->id, 1);
01019
01020
01021 if (base->data_class &&
01022 cgi_write_dataclass (base->id, base->data_class))
01023 error_exit ("cgi_write_dataclass", -1);
01024
01025
01026 if (base->units) create_node (base->units->id, base->id, 1);
01027
01028
01029 if (base->converg) create_node (base->converg->id, base->id, 1);
01030
01031
01032 if (base->equations) create_node (base->equations->id, base->id, 1);
01033
01034
01035 for (n=0; n<base->nintegrals; n++)
01036 create_node (base->integral[n].id, base->id, 1);
01037
01038 if (keep_nodes || FileVersion >= 2000) {
01039
01040 if (base->type) {
01041 const char *sim_name;
01042 sim_name = cg_SimulationTypeName(base->type);
01043 dim_vals = strlen(sim_name);
01044 if (cgio_new_node (outcgio, base->id, "SimulationType",
01045 "SimulationType_t","C1", 1,
01046 &dim_vals, sim_name, &base->type_id))
01047 error_exit ("cgio_new_node", 1);
01048 }
01049
01050
01051 if (base->biter) create_node (base->biter->id, base->id, 1);
01052
01053
01054 if (keep_nodes || FileVersion >= 2100) {
01055 for (n=0; n<base->nuser_data; n++)
01056 create_node (base->user_data[n].id, base->id, 1);
01057 }
01058 }
01059 }
01060 }
01061
01062
01063
01064 static char *temporary_file (char *basename)
01065 {
01066 char *p, *temp;
01067 int n;
01068
01069 if (basename == NULL || !*basename)
01070 basename = "cgnstmpfile";
01071 n = strlen (basename);
01072 temp = (char *) malloc (n + 10);
01073 if (temp == NULL) {
01074 fprintf (stderr, "malloc failed for temp filename\n");
01075 exit (1);
01076 }
01077 sprintf (temp, "%s.tmp", basename);
01078 p = temp + strlen(temp);
01079 for (n = 0; n < 1000; n++) {
01080 sprintf (p, "%3.3d~", n);
01081 if (access (temp, 0)) return temp;
01082 }
01083 fprintf (stderr, "failed to create temporary filename\n");
01084 exit (1);
01085 }
01086
01087
01088
01089 int main (int argc, char **argv)
01090 {
01091 int n, inpfn, filetype;
01092 float file_version;
01093
01094 if (argc < 3)
01095 print_usage (usgmsg, NULL);
01096
01097 while ((n = getargs (argc, argv, options)) > 0) {
01098 switch (n) {
01099 case 'v':
01100 verbose = 1;
01101 break;
01102 case 'r':
01103 keep_links = 0;
01104 break;
01105 case 'k':
01106 keep_nodes = 1;
01107 break;
01108 }
01109 }
01110
01111 if (argind >= argc - 1)
01112 print_usage (usgmsg, "version and/or CGNSfile not given");
01113
01114
01115
01116 FloatVersion = (float)atof(argv[argind++]);
01117 FileVersion = (int)(1000.0 * FloatVersion + 0.5);
01118
01119 if (FileVersion < 1200) {
01120 fprintf (stderr,
01121 "ADF incompatibilities do not allow versions prior to 1.2\n");
01122 exit (1);
01123 }
01124
01125 for (n = 0; n < nVersions; n++) {
01126 if (FileVersion == VersionList[n]) break;
01127 }
01128 if (n >= nVersions || VersionList[n] > LibraryVersion) {
01129 fprintf (stderr, "Version %g is not valid\n", FloatVersion);
01130 fprintf (stderr, "Version is one of 1.2");
01131 for (n = 1; n < nVersions; n++) {
01132 if (VersionList[n] < LibraryVersion)
01133 fprintf (stderr, ", %g", 0.001 * (float)VersionList[n]);
01134 }
01135 fprintf (stderr, " or %g\n", 0.001 * (float)LibraryVersion);
01136 exit (1);
01137 }
01138
01139 inpfile = argv[argind++];
01140 if (access (inpfile, 0)) {
01141 fprintf (stderr, "input file \"%s\" not found\n", inpfile);
01142 exit (1);
01143 }
01144
01145
01146
01147
01148 if (cg_open (inpfile, CG_MODE_READ, &inpfn)) cg_error_exit();
01149
01150
01151
01152 if (cg_version (inpfn, &file_version)) cg_error_exit();
01153 FromVersion = (int)(1000.0 * file_version + 0.5);
01154 if (LibraryVersion < FromVersion) {
01155 cg_close (inpfn);
01156 fprintf (stderr,
01157 "file version is more recent than then CGNS library version\n");
01158 exit (1);
01159 }
01160 if (FileVersion == FromVersion) {
01161 cg_close (inpfn);
01162 fprintf (stderr, "file version is already at version %g\n",
01163 FloatVersion);
01164 exit (1);
01165 }
01166
01167 printf ("converting \"%s\" from version %g to %g\n",
01168 inpfile, file_version, FloatVersion);
01169
01170 if (FileVersion < 2100) keep_links = 0;
01171
01172
01173
01174 cgfile = cgi_get_file(inpfn);
01175 if (cgio_get_root_id(cgfile->cgio, &inproot))
01176 cgio_error_exit("cgio_get_root_id");
01177 if (cgio_get_file_type(cgfile->cgio, &filetype))
01178 cgio_error_exit("cgio_get_file_type");
01179
01180
01181
01182 outfile = temporary_file (inpfile);
01183 printf ("writing output to temporary file \"%s\"\n", outfile);
01184
01185
01186
01187 if (cgio_open_file(outfile, 'w', filetype, &outcgio))
01188 cgio_error_exit("cgio_file_open");
01189 if (cgio_get_root_id(outcgio, &outroot))
01190 cgio_error_exit("cgio_get_root_id");
01191
01192 inpcgio = cgfile->cgio;
01193 cgfile->cgio = outcgio;
01194 cgfile->rootid = outroot;
01195 CGI_write ();
01196
01197
01198
01199 cgio_close_file(inpcgio);
01200 cgio_close_file(outcgio);
01201
01202
01203
01204 if (argind < argc)
01205 inpfile = argv[argind];
01206 printf ("renaming \"%s\" to \"%s\"\n", outfile, inpfile);
01207
01208 unlink (inpfile);
01209 if (rename (outfile, inpfile)) {
01210 fprintf (stderr, "rename %s -> %s failed", outfile, inpfile);
01211 exit (1);
01212 }
01213 free (outfile);
01214
01215 return 0;
01216 }
01217