| 1 |
/* |
| 2 |
* Copyright(C) 1999-2023 National Technology & Engineering Solutions |
| 3 |
* of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 4 |
* NTESS, the U.S. Government retains certain rights in this software. |
| 5 |
* |
| 6 |
* See packages/seacas/LICENSE for details |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <assert.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
#include "exodusII.h" |
| 15 |
|
| 16 |
/* |
| 17 |
// Read and EXODUSII database and return a TECPLOT file |
| 18 |
*/ |
| 19 |
void tec(int exoid, const char *filename) |
| 20 |
{ |
| 21 |
void teczone(int, int, int, char *, int, int, int *, int, double **, int, double **, FILE *); |
| 22 |
|
| 23 |
/* |
| 24 |
* FIRST, READ THE EXODUS DATA BASE |
| 25 |
*/ |
| 26 |
|
| 27 |
/* |
| 28 |
* Open the output file, if we can |
| 29 |
*/ |
| 30 |
FILE *tecfile = fopen(filename, "w"); |
| 31 |
if (tecfile == NULL) { |
| 32 |
printf("\nCannot open file %s for writing\n\n", filename); |
| 33 |
exit(1); |
| 34 |
} |
| 35 |
|
| 36 |
/* |
| 37 |
* Determine max name size used in database... |
| 38 |
*/ |
| 39 |
int name_size = ex_inquire_int(exoid, EX_INQ_DB_MAX_USED_NAME_LENGTH); |
| 40 |
ex_set_max_name_length(exoid, name_size); |
| 41 |
|
| 42 |
/* |
| 43 |
* Read database size, get coordinates and connectivity |
| 44 |
*/ |
| 45 |
char title[MAX_LINE_LENGTH + 1]; |
| 46 |
memset(title, 0, MAX_LINE_LENGTH + 1); |
| 47 |
int ndim, nnode, nelem, nblk, nnset, neset; |
| 48 |
ex_get_init(exoid, title, &ndim, &nnode, &nelem, &nblk, &nnset, &neset); |
| 49 |
|
| 50 |
double *x[3]; |
| 51 |
x[0] = x[1] = x[2] = NULL; |
| 52 |
|
| 53 |
char *nameco[3]; |
| 54 |
for (int i = 0; i < ndim; i++) { |
| 55 |
nameco[i] = (char *)malloc((name_size + 1) * sizeof(char)); |
| 56 |
x[i] = (double *)malloc(nnode * sizeof(double)); |
| 57 |
} |
| 58 |
ex_get_coord_names(exoid, nameco); |
| 59 |
if (strlen(nameco[0]) == 0) |
| 60 |
strcpy(nameco[0], "X"); |
| 61 |
if (strlen(nameco[1]) == 0) |
| 62 |
strcpy(nameco[1], "Y"); |
| 63 |
if (ndim > 2) |
| 64 |
if (strlen(nameco[2]) == 0) |
| 65 |
strcpy(nameco[2], "Z"); |
| 66 |
ex_get_coord(exoid, x[0], x[1], x[2]); |
| 67 |
|
| 68 |
int *elem_id = (int *)malloc(nblk * sizeof(int)); |
| 69 |
int *node_per_elem = (int *)malloc(nblk * sizeof(int)); |
| 70 |
int *elem_per_blk = (int *)malloc(nblk * sizeof(int)); |
| 71 |
int *attr_per_blk = (int *)malloc(nblk * sizeof(int)); |
| 72 |
char **elem_type = (char **)malloc(nblk * sizeof(char *)); |
| 73 |
int **icon = (int **)malloc(nblk * sizeof(int *)); |
| 74 |
for (int i = 0; i < nblk; i++) |
| 75 |
elem_type[i] = (char *)malloc((name_size + 1) * sizeof(char)); |
| 76 |
ex_get_ids(exoid, EX_ELEM_BLOCK, elem_id); |
| 77 |
for (int i = 0; i < nblk; i++) { |
| 78 |
ex_get_block(exoid, EX_ELEM_BLOCK, elem_id[i], elem_type[i], &elem_per_blk[i], |
| 79 |
&node_per_elem[i], NULL, NULL, &attr_per_blk[i]); |
| 80 |
|
| 81 |
icon[i] = (int *)malloc(elem_per_blk[i] * node_per_elem[i] * sizeof(int)); |
| 82 |
ex_get_conn(exoid, EX_ELEM_BLOCK, elem_id[i], icon[i], NULL, NULL); |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* Read time step information |
| 87 |
*/ |
| 88 |
double *time = NULL; |
| 89 |
int ntime = ex_inquire_int(exoid, EX_INQ_TIME); |
| 90 |
if (ntime > 0) { |
| 91 |
time = (double *)malloc(ntime * sizeof(double)); |
| 92 |
ex_get_all_times(exoid, time); |
| 93 |
} |
| 94 |
|
| 95 |
/* |
| 96 |
* Read number of nodal variables and save space |
| 97 |
*/ |
| 98 |
int nvar = 0; |
| 99 |
ex_get_variable_param(exoid, EX_NODAL, &nvar); |
| 100 |
|
| 101 |
char **varnames = NULL; |
| 102 |
double **q = NULL; |
| 103 |
if (nvar > 0) { |
| 104 |
varnames = (char **)malloc(nvar * sizeof(char *)); |
| 105 |
q = (double **)malloc(nvar * sizeof(double *)); |
| 106 |
for (int i = 0; i < nvar; i++) { |
| 107 |
varnames[i] = (char *)malloc((name_size + 1) * sizeof(char)); |
| 108 |
q[i] = (double *)malloc(nnode * sizeof(double)); |
| 109 |
} |
| 110 |
ex_get_variable_names(exoid, EX_NODAL, nvar, varnames); |
| 111 |
} |
| 112 |
|
| 113 |
/* ///////////////////////////////////////////////////////////////////// |
| 114 |
// PROMPT USER FOR INFO AND WRITE TECPLOT FILE |
| 115 |
///////////////////////////////////////////////////////////////////// |
| 116 |
*/ |
| 117 |
|
| 118 |
/* |
| 119 |
* Write the TECPLOT header information |
| 120 |
*/ |
| 121 |
|
| 122 |
assert(strlen(title) < (MAX_LINE_LENGTH + 1)); |
| 123 |
fprintf(tecfile, "TITLE = \"%s\"\n", title); |
| 124 |
fprintf(tecfile, "VARIABLES = "); |
| 125 |
for (int i = 0; i < ndim; i++) { |
| 126 |
fprintf(tecfile, "\"%s\"", nameco[i]); |
| 127 |
if (i < (ndim - 1)) |
| 128 |
fprintf(tecfile, ", "); |
| 129 |
} |
| 130 |
if (nvar == 0) |
| 131 |
fprintf(tecfile, "\n"); |
| 132 |
else |
| 133 |
fprintf(tecfile, ",\n "); |
| 134 |
|
| 135 |
int idum = 0; |
| 136 |
for (int i = 0; i < nvar; i++) { |
| 137 |
idum += strlen(varnames[i]); |
| 138 |
assert(idum < 1022); |
| 139 |
|
| 140 |
fprintf(tecfile, "\"%s\"", varnames[i]); |
| 141 |
if (i < (nvar - 1)) { |
| 142 |
if ((i + 1) % 4 == 0) { |
| 143 |
idum = 0; |
| 144 |
fprintf(tecfile, ",\n "); |
| 145 |
} |
| 146 |
else |
| 147 |
fprintf(tecfile, ", "); |
| 148 |
} |
| 149 |
} |
| 150 |
fprintf(tecfile, "\n"); |
| 151 |
|
| 152 |
/* |
| 153 |
* Select a time step |
| 154 |
*/ |
| 155 |
int izone = 0; |
| 156 |
int itime = 0; |
| 157 |
if (ntime == 0) { |
| 158 |
printf("\nNo solution variables available, saving mesh only\n\n"); |
| 159 |
izone = 1; |
| 160 |
} |
| 161 |
else { |
| 162 |
printf("\nTime step information:\n\n"); |
| 163 |
for (int i = 0; i < ntime; i++) |
| 164 |
printf(" Time step %5d, time = %e\n", i + 1, time[i]); |
| 165 |
do { |
| 166 |
printf("\nSelect time step number to save,\n"); |
| 167 |
printf(" or 0 for zone animation of all time steps: "); |
| 168 |
scanf("%d", &itime); |
| 169 |
printf("\n"); |
| 170 |
} while (itime < 0 || itime > ntime); |
| 171 |
printf("\n"); |
| 172 |
if (itime == 0) |
| 173 |
izone = 0; |
| 174 |
else |
| 175 |
izone = 1; |
| 176 |
} |
| 177 |
|
| 178 |
/* |
| 179 |
* Write time steps |
| 180 |
*/ |
| 181 |
|
| 182 |
if (izone == 0) { |
| 183 |
|
| 184 |
/* |
| 185 |
* Collapse the zones into one |
| 186 |
*/ |
| 187 |
|
| 188 |
/* |
| 189 |
* Make sure we are using all the same element types |
| 190 |
* Create one master connectivity array |
| 191 |
*/ |
| 192 |
for (int i = 1; i < nblk; i++) |
| 193 |
if (strcmp(elem_type[0], elem_type[i]) != 0) { |
| 194 |
printf("\nCannot create zone animation because\n"); |
| 195 |
; |
| 196 |
printf("\n there are multiple element types."); |
| 197 |
exit(1); |
| 198 |
} |
| 199 |
int *ic = (int *)malloc(nelem * node_per_elem[0] * sizeof(int)); |
| 200 |
int k = 0; |
| 201 |
for (int j = 0; j < nblk; j++) |
| 202 |
for (int i = 0; i < node_per_elem[j] * elem_per_blk[j]; i++) |
| 203 |
ic[k++] = icon[j][i]; |
| 204 |
assert(k == nelem * node_per_elem[0]); |
| 205 |
|
| 206 |
if (itime == 0) { |
| 207 |
for (int j = 0; j < ntime; j++) { |
| 208 |
for (int i = 0; i < nvar; i++) { |
| 209 |
ex_get_var(exoid, j + 1, EX_NODAL, i + 1, 1, nnode, q[i]); |
| 210 |
} |
| 211 |
|
| 212 |
int i = 0; |
| 213 |
teczone(1, nnode, j + 1, elem_type[i], node_per_elem[i], nelem, ic, ndim, x, nvar, q, |
| 214 |
tecfile); |
| 215 |
} |
| 216 |
printf("\n"); |
| 217 |
} |
| 218 |
|
| 219 |
free(ic); |
| 220 |
} |
| 221 |
else if (izone == 1) { |
| 222 |
|
| 223 |
/* |
| 224 |
|| Write out each zone individually |
| 225 |
*/ |
| 226 |
for (int i = 0; i < nvar; i++) |
| 227 |
ex_get_var(exoid, itime, EX_NODAL, i + 1, 1, nnode, q[i]); |
| 228 |
|
| 229 |
for (int i = 0; i < nblk; i++) |
| 230 |
teczone(nblk, nnode, elem_id[i], elem_type[i], node_per_elem[i], elem_per_blk[i], icon[i], |
| 231 |
ndim, x, nvar, q, tecfile); |
| 232 |
printf("\n"); |
| 233 |
} |
| 234 |
|
| 235 |
/* ///////////////////////////////////////////////////////////////////// |
| 236 |
// CLEAN UP |
| 237 |
///////////////////////////////////////////////////////////////////// |
| 238 |
*/ |
| 239 |
|
| 240 |
fclose(tecfile); |
| 241 |
|
| 242 |
/* |
| 243 |
* Free up allocated memory |
| 244 |
*/ |
| 245 |
for (int i = 0; i < ndim; i++) { |
| 246 |
free(nameco[i]); |
| 247 |
free(x[i]); |
| 248 |
} |
| 249 |
free(elem_id); |
| 250 |
free(node_per_elem); |
| 251 |
free(elem_per_blk); |
| 252 |
free(attr_per_blk); |
| 253 |
if (elem_type != NULL) { |
| 254 |
for (int i = 0; i < nblk; i++) { |
| 255 |
free(elem_type[i]); |
| 256 |
} |
| 257 |
free(elem_type); |
| 258 |
} |
| 259 |
if (icon != NULL) { |
| 260 |
for (int i = 0; i < nblk; i++) { |
| 261 |
free(icon[i]); |
| 262 |
} |
| 263 |
free(icon); |
| 264 |
} |
| 265 |
if (nvar > 0) { |
| 266 |
if (varnames != NULL) { |
| 267 |
for (int i = 0; i < nvar; i++) { |
| 268 |
free(varnames[i]); |
| 269 |
} |
| 270 |
free(varnames); |
| 271 |
} |
| 272 |
if (q != NULL) { |
| 273 |
for (int i = 0; i < nvar; i++) { |
| 274 |
free(q[i]); |
| 275 |
} |
| 276 |
free(q); |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
/* |
| 282 |
* Write TECPLOT zonal information |
| 283 |
*/ |
| 284 |
void teczone(int nblk, int nnode, int elem_id, char *elem_type, int node_per_elem, int elem_per_blk, |
| 285 |
int *icon, int ndim, double **x, int nvar, double **q, FILE *tecfile) |
| 286 |
{ |
| 287 |
int *ic = NULL; |
| 288 |
double *xx[3]; |
| 289 |
double **qq = NULL; |
| 290 |
int *isort = NULL; |
| 291 |
int inode; |
| 292 |
|
| 293 |
void internal_heapsort(int *, int); |
| 294 |
int hunt(int, int, int *, int *); |
| 295 |
|
| 296 |
int ifac = 1; |
| 297 |
if (strcmp(elem_type, "SHELL") == 0) { |
| 298 |
ifac = 2; |
| 299 |
} |
| 300 |
if (strcmp(elem_type, "BAR") == 0) { |
| 301 |
ifac = 2; |
| 302 |
} |
| 303 |
if (strcmp(elem_type, "TRUSS") == 0) { |
| 304 |
ifac = 4; |
| 305 |
} |
| 306 |
|
| 307 |
#ifdef DEBUG |
| 308 |
printf("...in teczone\n"); |
| 309 |
printf("......elem_type %s\n", elem_type); |
| 310 |
printf("......elem_per_blk %d\n", elem_per_blk); |
| 311 |
#endif |
| 312 |
if (nblk > 1) { |
| 313 |
/* |
| 314 |
* Create local block connectivity |
| 315 |
*/ |
| 316 |
ic = (int *)malloc(ifac * node_per_elem * elem_per_blk * sizeof(int)); |
| 317 |
isort = (int *)malloc(node_per_elem * elem_per_blk * sizeof(int)); |
| 318 |
|
| 319 |
for (int j = 0; j < node_per_elem * elem_per_blk; j++) |
| 320 |
isort[j] = icon[j]; |
| 321 |
|
| 322 |
#ifdef DEBUG |
| 323 |
printf("...sorting global connectivity\n"); |
| 324 |
printf("......total nodes %d\n", elem_per_blk * node_per_elem); |
| 325 |
#endif |
| 326 |
/* |
| 327 |
* Sort nodes in this element block |
| 328 |
*/ |
| 329 |
|
| 330 |
internal_heapsort(isort, node_per_elem * elem_per_blk); |
| 331 |
|
| 332 |
#ifdef DEBUG |
| 333 |
printf("...compressing global connectivity\n"); |
| 334 |
#endif |
| 335 |
/* |
| 336 |
* Compress node list |
| 337 |
*/ |
| 338 |
|
| 339 |
inode = 0; |
| 340 |
for (int j = 0; j < node_per_elem * elem_per_blk - 1; j++) |
| 341 |
if (isort[j] != isort[j + 1]) |
| 342 |
isort[++inode] = isort[j + 1]; |
| 343 |
inode++; |
| 344 |
|
| 345 |
#ifdef DEBUG |
| 346 |
printf("......found %d unique nodes\n", inode); |
| 347 |
printf("...sorting local connectivity\n"); |
| 348 |
#endif |
| 349 |
/* |
| 350 |
* Sort local connectivity |
| 351 |
*/ |
| 352 |
int i = (inode + 1) / 2; |
| 353 |
for (int k = 0; k < elem_per_blk; k++) { |
| 354 |
for (int j = 0; j < node_per_elem; j++) { |
| 355 |
int n = k * node_per_elem + j; |
| 356 |
int nn = k * ifac * node_per_elem + j; |
| 357 |
|
| 358 |
ic[nn] = hunt(icon[n], inode, isort, &i); |
| 359 |
} |
| 360 |
if (ifac == 2) |
| 361 |
for (int j = 0; j < node_per_elem; j++) { |
| 362 |
int nn = k * ifac * node_per_elem + j; |
| 363 |
ic[nn + node_per_elem] = ic[nn]; |
| 364 |
} |
| 365 |
else if (ifac == 4) |
| 366 |
for (int j = 0; j < node_per_elem; j++) { |
| 367 |
int nn = k * ifac * node_per_elem + j; |
| 368 |
ic[nn + node_per_elem] = ic[nn + node_per_elem - 1 - j]; |
| 369 |
ic[nn + 2 * node_per_elem] = ic[nn]; |
| 370 |
ic[nn + 3 * node_per_elem] = ic[nn + node_per_elem - 1 - j]; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
#ifdef DEBUG |
| 375 |
printf("...copy local data\n"); |
| 376 |
#endif |
| 377 |
/* |
| 378 |
* Copy local data |
| 379 |
*/ |
| 380 |
for (int ii = 0; ii < ndim; ii++) |
| 381 |
xx[ii] = (double *)malloc(inode * sizeof(double)); |
| 382 |
|
| 383 |
for (int j = 0; j < ndim; j++) |
| 384 |
for (int ii = 0; ii < inode; ii++) |
| 385 |
xx[j][ii] = x[j][isort[ii] - 1]; |
| 386 |
|
| 387 |
if (nvar > 0) |
| 388 |
qq = (double **)malloc(nvar * sizeof(double *)); |
| 389 |
for (int ii = 0; ii < nvar; ii++) |
| 390 |
qq[ii] = (double *)malloc(inode * sizeof(double)); |
| 391 |
|
| 392 |
for (int j = 0; j < nvar; j++) |
| 393 |
for (int ii = 0; ii < inode; ii++) |
| 394 |
qq[j][ii] = q[j][isort[ii] - 1]; |
| 395 |
} |
| 396 |
else { |
| 397 |
/* |
| 398 |
* Copy local block connectivity |
| 399 |
*/ |
| 400 |
|
| 401 |
if (ifac > 1) { |
| 402 |
printf("\nCannot perform zone animation for degenerate elements\n\n"); |
| 403 |
exit(1); |
| 404 |
} |
| 405 |
|
| 406 |
inode = nnode; |
| 407 |
ic = icon; |
| 408 |
|
| 409 |
for (int i = 0; i < ndim; i++) |
| 410 |
xx[i] = x[i]; |
| 411 |
|
| 412 |
if (nvar > 0) |
| 413 |
qq = (double **)malloc(nvar * sizeof(double *)); |
| 414 |
for (int i = 0; i < nvar; i++) |
| 415 |
qq[i] = q[i]; |
| 416 |
} |
| 417 |
|
| 418 |
#ifdef DEBUG |
| 419 |
printf("...writing zone data\n"); |
| 420 |
#endif |
| 421 |
/* |
| 422 |
* Write zone data |
| 423 |
*/ |
| 424 |
printf(" Writing Zone %4d, %8s, %6d elements, %6d nodes\n", elem_id, elem_type, elem_per_blk, |
| 425 |
inode); |
| 426 |
char zname[80]; |
| 427 |
char eltype[16]; |
| 428 |
snprintf(zname, 80, "Zone %s_%d", elem_type, elem_id); |
| 429 |
|
| 430 |
if (ndim == 3 && (node_per_elem == 8 || (node_per_elem == 4 && ifac == 2) || |
| 431 |
(node_per_elem == 2 && ifac == 4))) |
| 432 |
snprintf(eltype, 16, "BRICK"); |
| 433 |
else if (ndim == 3 && node_per_elem == 4) |
| 434 |
snprintf(eltype, 16, "TETRAHEDRON"); |
| 435 |
else if (ndim == 2 && (node_per_elem == 4 || (node_per_elem == 2 && ifac == 2))) |
| 436 |
snprintf(eltype, 16, "QUADRILATERAL"); |
| 437 |
else if (ndim == 2 && node_per_elem == 3) |
| 438 |
snprintf(eltype, 16, "TRIANGLE"); |
| 439 |
else { |
| 440 |
printf("\nBad element type found in teczone\n"); |
| 441 |
printf(" Dimensions = %d\n", ndim); |
| 442 |
printf(" Nodes = %d\n", node_per_elem); |
| 443 |
exit(1); |
| 444 |
} |
| 445 |
|
| 446 |
fprintf(tecfile, "ZONE T=\"%s\", F=FEBLOCK, ET=%s,\n", zname, eltype); |
| 447 |
fprintf(tecfile, " N=%d, E=%d\n", inode, elem_per_blk); |
| 448 |
|
| 449 |
#define CHUNK 8 |
| 450 |
|
| 451 |
for (int j = 0; j < ndim; j++) { |
| 452 |
for (int i = 0; i < inode / CHUNK; i++) { |
| 453 |
for (int k = 0; k < CHUNK; k++) |
| 454 |
fprintf(tecfile, "%.16f ", xx[j][i * CHUNK + k]); |
| 455 |
fprintf(tecfile, "\n"); |
| 456 |
} |
| 457 |
if (inode % CHUNK != 0) { |
| 458 |
for (int i = CHUNK * (inode / CHUNK); i < inode; i++) |
| 459 |
fprintf(tecfile, "%.16f ", xx[j][i]); |
| 460 |
fprintf(tecfile, "\n"); |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
for (int j = 0; j < nvar; j++) { |
| 465 |
for (int i = 0; i < inode / CHUNK; i++) { |
| 466 |
for (int k = 0; k < CHUNK; k++) |
| 467 |
fprintf(tecfile, "%.16f ", qq[j][i * CHUNK + k]); |
| 468 |
fprintf(tecfile, "\n"); |
| 469 |
} |
| 470 |
if (inode % CHUNK != 0) { |
| 471 |
for (int i = CHUNK * (inode / CHUNK); i < inode; i++) |
| 472 |
fprintf(tecfile, "%.16f ", qq[j][i]); |
| 473 |
fprintf(tecfile, "\n"); |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
for (int i = 0; i < elem_per_blk; i++) { |
| 478 |
for (int j = 0; j < ifac * node_per_elem; j++) { |
| 479 |
int k = ic[i * ifac * node_per_elem + j]; |
| 480 |
fprintf(tecfile, "%d ", k); |
| 481 |
} |
| 482 |
fprintf(tecfile, "\n"); |
| 483 |
} |
| 484 |
|
| 485 |
#ifdef DEBUG |
| 486 |
printf("...freeing allocated space\n"); |
| 487 |
#endif |
| 488 |
if (nblk > 1) { |
| 489 |
free(ic); |
| 490 |
free(isort); |
| 491 |
for (int i = 0; i < ndim; i++) |
| 492 |
free(xx[i]); |
| 493 |
if (qq != NULL) { |
| 494 |
for (int i = 0; i < nvar; i++) |
| 495 |
free(qq[i]); |
| 496 |
if (nvar > 0) |
| 497 |
free(qq); |
| 498 |
} |
| 499 |
} |
| 500 |
else { |
| 501 |
if (nvar > 0) |
| 502 |
free(qq); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
void siftDown(int *a, int start, int count); |
| 507 |
|
| 508 |
#define SWAP(r, s) \ |
| 509 |
do { \ |
| 510 |
int t = r; \ |
| 511 |
r = s; \ |
| 512 |
s = t; \ |
| 513 |
} while (0) |
| 514 |
|
| 515 |
void internal_heapsort(int *a, int count) |
| 516 |
{ |
| 517 |
/* heapify */ |
| 518 |
for (int start = (count - 2) / 2; start >= 0; start--) { |
| 519 |
siftDown(a, start, count); |
| 520 |
} |
| 521 |
|
| 522 |
for (int end = count - 1; end > 0; end--) { |
| 523 |
SWAP(a[end], a[0]); |
| 524 |
siftDown(a, 0, end); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
void siftDown(int *a, int start, int end) |
| 529 |
{ |
| 530 |
int root = start; |
| 531 |
|
| 532 |
while (root * 2 + 1 < end) { |
| 533 |
int child = 2 * root + 1; |
| 534 |
if ((child + 1 < end) && (a[child] < a[child + 1])) { |
| 535 |
child += 1; |
| 536 |
} |
| 537 |
if (a[root] < a[child]) { |
| 538 |
SWAP(a[child], a[root]); |
| 539 |
root = child; |
| 540 |
} |
| 541 |
else |
| 542 |
return; |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
/* |
| 547 |
* Bisection search with binary hunt, |
| 548 |
* from Numerical Recipes |
| 549 |
*/ |
| 550 |
int hunt(int node, int n, int *key, int *iguess) |
| 551 |
{ |
| 552 |
if (node == key[*iguess - 1]) |
| 553 |
return *iguess; |
| 554 |
int i = *iguess; |
| 555 |
|
| 556 |
int inc = 1; |
| 557 |
int j = 0; |
| 558 |
|
| 559 |
if (node > key[i - 1]) { /* hunt up */ |
| 560 |
|
| 561 |
if (i == n) { |
| 562 |
printf("\nError in HUNT: node is outside range of table\n"); |
| 563 |
printf(" requested node = %d\n", node); |
| 564 |
printf(" last in table = %d\n", key[n - 1]); |
| 565 |
exit(1); |
| 566 |
} |
| 567 |
|
| 568 |
j = i + 1; |
| 569 |
while (node > key[j - 1]) { |
| 570 |
i = j; |
| 571 |
inc += inc; |
| 572 |
j = i + inc; |
| 573 |
if (j > n) { |
| 574 |
j = n; |
| 575 |
break; |
| 576 |
} |
| 577 |
} |
| 578 |
if (node == key[j - 1]) { |
| 579 |
*iguess = j; |
| 580 |
return j; |
| 581 |
} |
| 582 |
} |
| 583 |
else if (node < key[i - 1]) { /* hunt down */ |
| 584 |
|
| 585 |
if (i == 1) { |
| 586 |
printf("\nError in HUNT: node is outside range of table\n"); |
| 587 |
printf(" requested node = %d\n", node); |
| 588 |
printf(" first in table = %d\n", key[0]); |
| 589 |
exit(1); |
| 590 |
} |
| 591 |
|
| 592 |
j = i; |
| 593 |
i -= 1; |
| 594 |
while (node < key[i - 1]) { |
| 595 |
j = i; |
| 596 |
inc += inc; |
| 597 |
i = j - inc; |
| 598 |
if (i < 1) { |
| 599 |
i = 1; |
| 600 |
break; |
| 601 |
} |
| 602 |
} |
| 603 |
if (node == key[i - 1]) { |
| 604 |
*iguess = i; |
| 605 |
return i; |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
int k = (i + j) / 2; |
| 610 |
while (node != key[k - 1]) { |
| 611 |
if (key[k - 1] > node) { |
| 612 |
j = k; |
| 613 |
k = (i + j) / 2; |
| 614 |
} |
| 615 |
else { |
| 616 |
i = k; |
| 617 |
k = (i + j + 1) / 2; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
*iguess = k; |
| 622 |
return k; |
| 623 |
} |