Debian Codegraph @

packages/seacas/libraries/exodus/test/create_mesh.c

raw
1 /*
2 * Copyright(C) 1999-2024 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 <inttypes.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "exodusII.h"
17
18 #define DEFAULT_FILE_NAME "mesh"
19 #define DEFAULT_MAP_ORIGIN 1
20 #define DEFAULT_NUM_DOMAINS 1
21 #define DEFAULT_NUM_ELEMENTS 1000000
22 #define DEFAULT_NUM_FIELDS 0
23 #define DEFAULT_NUM_TIMESTEPS 1
24
25 #define MAX_STRING_LEN 128
26 #define NUM_BYTES_PER_INT 4
27 #define NUM_NODES_PER_ELEM 8
28
29 #define EBLK_ID 100000
30 #define EXODUSII_FILE_TYPE ".e"
31
32 typedef double realtyp;
33
34 typedef int64_t INT;
35
36 INT StringToCount(char *size_str)
37 {
38 INT size = 0;
39 char range;
40 int rc;
41
42 rc = sscanf(size_str, "%" PRId64 "%c", &size, &range);
43 if (rc == 2) {
44 switch ((int)range) {
45 case 'k':
46 case 'K': size *= 1000; break;
47 case 'm':
48 case 'M': size *= 1000000; break;
49 case 'g':
50 case 'G': size *= 1000000000; break;
51 }
52 }
53 else if (rc == 0) {
54 size = -1;
55 }
56 return size;
57 } /* StringToCount() */
58
59 void get_file_name(const char *base, const char *ext, int rank, int nprocs, const char *other,
60 char *output);
61
62 /* We need to do a cube-root to find the number of elements on each
63 * side of the cube, but don't want to link in -lm just for
64 * this. Use this routine which is better than
65 * a brute force approach. Found at
66 * http://www.hackersdelight.org/HDcode/icbrt.c
67 */
68 INT icbrt(unsigned x)
69 {
70 INT s = 30;
71 unsigned y = 0;
72 while (s >= 0) { /* Do 11 times. */
73 y = 2 * y;
74 unsigned b = (3 * y * (y + 1) + 1) << s;
75 s = s - 3;
76 if (x >= b) {
77 x = x - b;
78 y = y + 1;
79 }
80 }
81 return y;
82 }
83
84 /* Prototypes */
85 void create_rr_elem_map(INT loc_num_elements, INT *elem_map, INT map_origin, INT num_domains,
86 INT current_domain);
87
88 void create_elem_map(INT loc_num_elems, INT elem_num, INT *elem_map, INT map_origin);
89
90 void create_local_connect(INT *node_map, INT len_node_map, INT len_connect, INT *domain_connect,
91 INT *loc_connect, INT map_origin);
92
93 void extract_connect(INT element_offset, INT num_elem, INT *elem_map, INT *connect,
94 INT *domain_connect, INT map_origin);
95
96 void make_mesh(realtyp *x, realtyp *y, realtyp *z, INT *connect, INT map_origin,
97 INT num_elements_1d);
98
99 void parse_input(int argc, char *argv[], bool *debug, INT *map_origin, INT *num_elements_1d,
100 INT *num_domains, INT *num_nodal_fields, INT *num_global_fields,
101 INT *num_element_fields, INT *num_timesteps, char *file_name, int *exodus,
102 int *compression_level, int *shuffle, int *int64bit);
103
104 void write_exo_mesh(int debug, char *file_name, INT map_origin, INT num_nodes, INT num_elements,
105 INT num_domains, INT num_nodal_fields, INT num_global_fields,
106 INT num_element_fields, INT num_timesteps, realtyp *x, realtyp *y, realtyp *z,
107 INT *connect, int compression_level, int shuffle, int int64bit);
108
109 void create_node_map(INT len_map, INT len_connect, INT *domain_connect, INT *node_map,
110 INT *loc_num_nodes, INT map_origin);
111
112 INT bin_search2(INT value, INT num, INT List[]);
113
114 /***********************************************************************
115 *
116 * Main function
117 *
118 ***********************************************************************/
119
120 int main(int argc, char *argv[])
121 {
122 INT *connect;
123 bool debug = false; /* true, display debug information; false */
124 /* otherwise. */
125 static char file_name[MAX_STRING_LEN] = DEFAULT_FILE_NAME;
126 int exodus = true;
127 INT map_origin = DEFAULT_MAP_ORIGIN;
128 INT num_domains = DEFAULT_NUM_DOMAINS;
129 INT num_elements_1d;
130 INT num_elements = DEFAULT_NUM_ELEMENTS;
131 INT num_nodal_fields = DEFAULT_NUM_FIELDS;
132 INT num_global_fields = DEFAULT_NUM_FIELDS;
133 INT num_element_fields = DEFAULT_NUM_FIELDS;
134 INT num_timesteps = DEFAULT_NUM_TIMESTEPS;
135 INT num_nodes;
136 int compression_level = 0;
137 int shuffle = 0;
138 int int64bit = 0;
139 size_t size;
140
141 ex_opts(EX_VERBOSE | EX_ABORT);
142
143 /* Parse Input */
144 parse_input(argc, argv, &debug, &map_origin, &num_elements, &num_domains, &num_nodal_fields,
145 &num_global_fields, &num_element_fields, &num_timesteps, file_name, &exodus,
146 &compression_level, &shuffle, &int64bit);
147
148 /* Create Coordinates and Connectivity Array */
149 num_elements_1d = icbrt(num_elements);
150 num_nodes = (num_elements_1d + 1) * (num_elements_1d + 1) * (num_elements_1d + 1);
151 realtyp *x = malloc(num_nodes * sizeof(realtyp));
152 realtyp *y = malloc(num_nodes * sizeof(realtyp));
153 realtyp *z = malloc(num_nodes * sizeof(realtyp));
154 assert(x != NULL && y != NULL && z != NULL);
155
156 num_elements = num_elements_1d * num_elements_1d * num_elements_1d;
157 size = (size_t)NUM_NODES_PER_ELEM * num_elements * sizeof(INT);
158 assert(size > 0);
159 connect = malloc(size);
160 assert(connect != NULL);
161
162 fprintf(stderr, "Creating a 3D mesh of %" PRId64 " hex elements and %" PRId64 " nodes.\n",
163 num_elements, num_nodes);
164
165 make_mesh(x, y, z, connect, map_origin, num_elements_1d);
166 fprintf(stderr, "\t...Mesh topology created.\n");
167
168 /*
169 * Write Out Mesh
170 */
171
172 if (exodus) {
173 write_exo_mesh(debug, file_name, map_origin, num_nodes, num_elements, num_domains,
174 num_nodal_fields, num_global_fields, num_element_fields, num_timesteps, x, y, z,
175 connect, compression_level, shuffle, int64bit);
176 }
177
178 free(x);
179 free(y);
180 free(z);
181 free(connect);
182 return 0;
183 } /* end of main() */
184
185 /***********************************************************************
186 ***********************************************************************/
187 void parse_input(int argc, char *argv[], bool *debug, INT *map_origin, INT *num_elements_1d,
188 INT *num_domains, INT *num_nodal_fields, INT *num_global_fields,
189 INT *num_element_fields, INT *num_timesteps, char *file_name, int *exodus,
190 int *compression_level, int *shuffle, int *int64bit)
191 {
192 int arg = 0; /* Argument index. */
193
194 while (++arg < argc) {
195 if (strcmp("-c", argv[arg]) == 0) {
196 if (++arg < argc) {
197 *num_nodal_fields = strtol(argv[arg], NULL, 10);
198 }
199 }
200 else if (strcmp("-compress", argv[arg]) == 0) {
201 if (++arg < argc) {
202 *compression_level = strtol(argv[arg], NULL, 10);
203 }
204 }
205 else if (strcmp("-shuffle", argv[arg]) == 0) {
206 *shuffle = 1;
207 }
208 else if (strcmp("-64", argv[arg]) == 0) {
209 *int64bit = 1;
210 }
211 else if (strcmp("-nv", argv[arg]) == 0) {
212 if (++arg < argc) {
213 *num_nodal_fields = strtol(argv[arg], NULL, 10);
214 }
215 }
216 else if (strcmp("-gv", argv[arg]) == 0) {
217 if (++arg < argc) {
218 *num_global_fields = strtol(argv[arg], NULL, 10);
219 }
220 }
221 else if (strcmp("-ev", argv[arg]) == 0) {
222 if (++arg < argc) {
223 *num_element_fields = strtol(argv[arg], NULL, 10);
224 }
225 }
226 else if (strcmp("-t", argv[arg]) == 0) {
227 if (++arg < argc) {
228 *num_timesteps = strtol(argv[arg], NULL, 10);
229 }
230 }
231 else if (strcmp("-d", argv[arg]) == 0) {
232 *debug = true;
233 }
234 else if (strcmp("-f", argv[arg]) == 0) {
235 if (++arg < argc) {
236 ex_copy_string(file_name, argv[arg], MAX_STRING_LEN);
237 }
238 }
239 else if (strcmp("-m", argv[arg]) == 0) {
240 if (++arg < argc) {
241 *map_origin = strtol(argv[arg], NULL, 10);
242 }
243 }
244 else if (strcmp("-n", argv[arg]) == 0) {
245 if (++arg < argc) {
246 *num_elements_1d = StringToCount(argv[arg]);
247 }
248 }
249 else if (strcmp("-p", argv[arg]) == 0) {
250 if (++arg < argc) {
251 *num_domains = strtol(argv[arg], NULL, 10);
252 }
253 }
254 else if (strcmp("-x", argv[arg]) == 0) {
255 *exodus = true;
256 }
257 else if ((strcmp("-h", argv[arg]) == 0) || (strcmp("-u", argv[arg]) == 0)) {
258 printf(" \n");
259 printf("NAME \n");
260 printf(" \n");
261 printf("create_mesh - creates a mesh file for performance benchmarking. \n");
262 printf(" \n");
263 printf("SYNOPSIS \n");
264 printf(" \n");
265 printf("create_mesh [-c fields] [-t timesteps] [-d] [-f file_name] \n");
266 printf(" [-m map_origin] [-n elements] [-p domains] \n");
267 printf(" [-nv number] [-ev number] [-gv number] ");
268 printf(" [-r] [-u] [-h] ");
269 printf("[-x]");
270 printf(" \n");
271 printf(" \n");
272 printf("DESCRIPTION \n");
273 printf(" \n");
274 printf("This program creates a 3-D mesh for performance benchmarking. \n");
275 printf("The EXODUSII II database file(s) created by this \n");
276 printf("prrogram is/are read by the rd_wt_mesh program to perform the \n");
277 printf("actual benchmark. \n");
278 printf(" \n");
279 printf("OPTIONS \n");
280 printf(" \n");
281 printf("-c fields number of nodal fields. Default: %d \n",
282 DEFAULT_NUM_FIELDS);
283 printf("-nv fields number of nodal fields. Default: %d \n",
284 DEFAULT_NUM_FIELDS);
285 printf("-ev fields number of element fields. Default: %d \n",
286 DEFAULT_NUM_FIELDS);
287 printf("-gv fields number of global fields. Default: %d \n",
288 DEFAULT_NUM_FIELDS);
289 printf("-t timesteps number of timesteps. Default: %d \n",
290 DEFAULT_NUM_TIMESTEPS);
291 printf("-d display debug information. \n");
292 printf("-f file_name file name prefix for all created files: \n");
293 printf(" \n");
294 printf(" 'file_name'_n%s [EXODUSII II file] \n",
295 EXODUSII_FILE_TYPE);
296 printf(" \n");
297 printf(" where n varies from 0 to number of domains-1. \n");
298 printf(" Default: %s \n",
299 DEFAULT_FILE_NAME);
300 printf("-h display help/usage information. \n");
301 printf("-m map_origin element map origin. Default: %d \n",
302 DEFAULT_MAP_ORIGIN);
303 printf("-n elements number of elements in mesh \n");
304 printf(" Can suffix with 'k', 'm', 'g' for thousand, million, billion\n");
305 printf(" elements/file = elements/number_of_domains. \n");
306 printf(" Default: %d \n",
307 DEFAULT_NUM_ELEMENTS);
308 printf("-p domains number of domains. Default: %d \n",
309 DEFAULT_NUM_DOMAINS);
310 printf("-compress val set compression to level 'val' [0..9] \n");
311 printf("-shuffle enable hdf5-shuffle \n");
312 printf("-64 enable 64-bit integers \n");
313 printf("-u display help/usage information. \n");
314
315 exit(0);
316 }
317 else {
318 fprintf(stderr, "Unknown option: %s\n", argv[arg]);
319 fprintf(stderr, "Enter create_mesh -h for description of valid options.\n");
320
321 exit(0);
322 }
323 }
324 }
325
326 /***********************************************************************
327 *
328 * Create the coordinates and connectivity array for the mesh
329 *
330 ***********************************************************************/
331
332 void make_mesh(realtyp *x, realtyp *y, realtyp *z, INT *connect, INT map_origin,
333 INT num_elements_1d)
334 {
335 /* create global coordinates */
336 size_t k = 0;
337 for (INT m = 0; m < (num_elements_1d + 1); m++) {
338 for (INT i = 0; i < (num_elements_1d + 1); i++) {
339 for (INT j = 0; j < (num_elements_1d + 1); j++, k++) {
340 x[k] = (realtyp)j;
341 y[k] = (realtyp)i;
342 z[k] = (realtyp)m;
343 }
344 }
345 }
346
347 /* build connectivity array (node list) for mesh */
348 size_t elp1sq = (num_elements_1d + 1) * (num_elements_1d + 1);
349 size_t cnt = 0;
350 for (INT m = 0; m < num_elements_1d; m++) {
351 k = 0;
352 for (INT i = 0; i < num_elements_1d; i++) {
353 for (INT j = 0; j < num_elements_1d; j++, k++) {
354 size_t base = (m * elp1sq) + k + i + map_origin;
355 connect[cnt++] = base;
356 connect[cnt++] = base + 1;
357 connect[cnt++] = base + num_elements_1d + 2;
358 connect[cnt++] = base + num_elements_1d + 1;
359
360 connect[cnt++] = elp1sq + base;
361 connect[cnt++] = elp1sq + base + 1;
362 connect[cnt++] = elp1sq + base + num_elements_1d + 2;
363 connect[cnt++] = elp1sq + base + num_elements_1d + 1;
364 }
365 }
366 }
367 } /* end of make_mesh() */
368
369 /***********************************************************************
370 ***********************************************************************/
371 void write_exo_mesh(int debug, char *file_name, INT map_origin, INT num_nodes, INT num_elements,
372 INT num_domains, INT num_nodal_fields, INT num_global_fields,
373 INT num_element_fields, INT num_timesteps, realtyp *x, realtyp *y, realtyp *z,
374 INT *connect, int compression_level, int shuffle, int int64bit)
375 {
376 realtyp *loc_xcoords = NULL;
377 realtyp *loc_ycoords = NULL;
378 realtyp *loc_zcoords = NULL;
379 realtyp *globals = NULL;
380
381 INT accum_num_elements = 0;
382 INT loc_num_elements, loc_num_nodes;
383 INT *elem_map = NULL;
384 INT *node_map = NULL;
385 INT *domain_connect = NULL;
386 INT *loc_connect = NULL;
387
388 for (INT i = 0; i < num_domains; i++) {
389 int mymode = EX_MAPS_INT64_API | EX_BULK_INT64_API | EX_IDS_INT64_API;
390 if (int64bit) {
391 mymode |= EX_MAPS_INT64_DB | EX_BULK_INT64_DB | EX_IDS_INT64_DB;
392 }
393
394 /* create the EXODUSII file */
395 char temporary_name[MAX_STRING_LEN];
396 get_file_name(file_name, "e", i, num_domains, NULL, temporary_name);
397
398 int CPU_word_size = sizeof(realtyp);
399 int IO_word_size = sizeof(realtyp);
400 int exoid = ex_create(temporary_name, EX_CLOBBER | mymode, &CPU_word_size, &IO_word_size);
401
402 if (exoid < 0) {
403 fprintf(stderr, "after ex_create, error = %d\n", exoid);
404 exit(-1);
405 }
406
407 ex_set_option(exoid, EX_OPT_COMPRESSION_LEVEL, compression_level);
408 ex_set_option(exoid, EX_OPT_COMPRESSION_SHUFFLE, shuffle);
409
410 if (num_domains > 1) {
411 /* Determine local number of elements */
412 if (num_elements < num_domains) {
413 fprintf(stderr, "number of elements is less than number of domains.\n");
414 if (i < num_elements) {
415 loc_num_elements = 1;
416 }
417 else {
418 loc_num_elements = 0;
419 }
420 }
421 else {
422 loc_num_elements = num_elements / num_domains;
423 if (i < (num_elements % num_domains)) {
424 loc_num_elements++;
425 }
426 }
427
428 /* malloc things we need */
429 INT len_connect = NUM_NODES_PER_ELEM * loc_num_elements;
430 if (i == 0) { /* first time through; max size arrays occur on
431 first iteration */
432 elem_map = malloc(loc_num_elements * sizeof(INT));
433 domain_connect = malloc(len_connect * sizeof(INT));
434 loc_connect = malloc(len_connect * sizeof(INT));
435 node_map = malloc(num_nodes * sizeof(INT));
436 }
437
438 /* Create element local/global map */
439 create_elem_map(loc_num_elements, accum_num_elements, elem_map, map_origin);
440
441 /* Extract current domain's connectivity, referencing global node ids */
442 extract_connect(accum_num_elements, loc_num_elements, elem_map, connect, domain_connect,
443 map_origin);
444
445 accum_num_elements += loc_num_elements;
446
447 /* The local/global node map is just the current domain's connectivity,
448 sorted with duplicate entries removed */
449 create_node_map(num_nodes, len_connect, domain_connect, node_map, &loc_num_nodes, map_origin);
450
451 /* Using local/global node map, convert the domain connectivity
452 (referencing global node ids) to local connectivity (referencing
453 local node ids) */
454
455 create_local_connect(node_map, loc_num_nodes, len_connect, domain_connect, loc_connect,
456 map_origin);
457 }
458 else {
459 loc_num_elements = num_elements;
460 loc_num_nodes = num_nodes;
461 }
462
463 if (debug) {
464 fprintf(stderr, "\n\n\n");
465
466 fprintf(stderr, "\n domain: %" PRId64 "\n", i);
467 fprintf(stderr, "\n loc_num_elements: %" PRId64 "\n", loc_num_elements);
468 fprintf(stderr, "\n loc_num_nodes: %" PRId64 "\n", loc_num_nodes);
469 }
470
471 int num_dim = 3;
472 int num_elem_blk = 1;
473 int num_node_sets = 0;
474 int num_side_sets = 0;
475
476 int err = ex_put_init(exoid, "This is an EXODUSII performance test.", num_dim, loc_num_nodes,
477 loc_num_elements, num_elem_blk, num_node_sets, num_side_sets);
478
479 if (err) {
480 fprintf(stderr, "after ex_put_init, error = %d\n", err);
481 ex_close(exoid);
482 exit(-1);
483 }
484
485 /* Extract the local x and y coordinates */
486 if (num_domains > 1) {
487 INT loc_node_size = -1;
488 if (loc_num_nodes > loc_node_size) {
489 realtyp *tmpx = realloc(loc_xcoords, loc_num_nodes * sizeof(realtyp));
490 if (tmpx == NULL) {
491 free(loc_xcoords);
492 fprintf(stderr, "error realloc'ing loc_xcoords\n");
493 ex_close(exoid);
494 exit(-1);
495 }
496 {
497 loc_xcoords = tmpx;
498 }
499 realtyp *tmpy = realloc(loc_ycoords, loc_num_nodes * sizeof(realtyp));
500 if (tmpy == NULL) {
501 free(loc_ycoords);
502 fprintf(stderr, "error realloc'ing loc_ycoords\n");
503 ex_close(exoid);
504 exit(-1);
505 }
506 {
507 loc_ycoords = tmpy;
508 }
509 realtyp *tmpz = realloc(loc_zcoords, loc_num_nodes * sizeof(realtyp));
510 if (tmpz == NULL) {
511 free(loc_zcoords);
512 fprintf(stderr, "error realloc'ing loc_zcoords\n");
513 ex_close(exoid);
514 exit(-1);
515 }
516 {
517 loc_zcoords = tmpz;
518 }
519 loc_node_size = loc_num_nodes;
520 }
521
522 for (INT j = 0; j < loc_num_nodes; j++) {
523 INT index = node_map[j] - map_origin;
524 loc_xcoords[j] = x[index];
525 loc_ycoords[j] = y[index];
526 loc_zcoords[j] = z[index];
527 }
528
529 err = ex_put_coord(exoid, loc_xcoords, loc_ycoords, loc_zcoords);
530 }
531 else {
532 err = ex_put_coord(exoid, x, y, z);
533 }
534 if (err) {
535 fprintf(stderr, "after ex_put_coord, error = %d\n", err);
536 ex_close(exoid);
537 exit(-1);
538 }
539 if (debug) {
540 fprintf(stderr, "\tCoordinates output.\n");
541 }
542 #if 1
543 {
544 INT ids[1] = {EBLK_ID};
545 INT num_elem_per_block[1];
546 char *names[1] = {"hex"};
547 INT num_node_per_elem[1];
548 INT num_attr_per_block[1];
549 bool write_map = num_domains > 1 ? true : false;
550 num_elem_per_block[0] = loc_num_elements;
551 num_node_per_elem[0] = NUM_NODES_PER_ELEM;
552 num_attr_per_block[0] = 0;
553 err = ex_put_concat_elem_block(exoid, ids, names, num_elem_per_block, num_node_per_elem,
554 num_attr_per_block, write_map);
555 }
556 #else
557 err = ex_put_block(exoid, EX_ELEM_BLOCK, 10000000000, "hex", loc_num_elements,
558 NUM_NODES_PER_ELEM, 0);
559 #endif
560
561 if (err) {
562 fprintf(stderr, "after ex_put_elem_block, error = %d\n", err);
563 ex_close(exoid);
564 exit(-1);
565 }
566
567 if (num_domains > 1) {
568 err = ex_put_conn(exoid, EX_ELEM_BLOCK, EBLK_ID, loc_connect, NULL, NULL);
569 }
570 else {
571 err = ex_put_conn(exoid, EX_ELEM_BLOCK, EBLK_ID, connect, NULL, NULL);
572 }
573
574 if (err) {
575 fprintf(stderr, "after ex_put_elem_conn, error = %d\n", err);
576 ex_close(exoid);
577 exit(-1);
578 }
579
580 if (debug) {
581 fprintf(stderr, "\tConnectivity output.\n");
582 }
583 /* write out element and node maps */
584
585 if (num_domains > 1) {
586 err = ex_put_id_map(exoid, EX_NODE_MAP, node_map);
587
588 if (err) {
589 fprintf(stderr, "after ex_put_id_map, error = %d\n", err);
590 ex_close(exoid);
591 exit(-1);
592 }
593
594 err = ex_put_id_map(exoid, EX_ELEM_MAP, elem_map);
595
596 if (err) {
597 fprintf(stderr, "after ex_put_id_map, error = %d\n", err);
598 ex_close(exoid);
599 exit(-1);
600 }
601
602 if (debug) {
603 fprintf(stderr, "\tMaps output.\n");
604 }
605 }
606
607 /* write out simulated results fields;
608 we'll just write out the x coordinate field 'num_nodal_fields' times */
609 if (loc_num_nodes < loc_num_elements) {
610 fprintf(stderr, "INTERNAL ERROR: Programmer assumed number of nodes > number of elements, "
611 "but that is not true.\n");
612 ex_close(exoid);
613 exit(-1);
614 }
615
616 int *elem_var_tab = NULL;
617 if (num_element_fields > 0) {
618 elem_var_tab = malloc(num_element_fields * sizeof(int));
619 for (INT j = 0; j < num_element_fields; j++) {
620 elem_var_tab[j] = 1;
621 }
622 }
623 err = ex_put_all_var_param(exoid, num_global_fields, num_nodal_fields, num_element_fields,
624 elem_var_tab, 0, 0, 0, 0);
625 if (err) {
626 fprintf(stderr, "after ex_put_all_var_param, error = %d\n", err);
627 ex_close(exoid);
628 exit(-1);
629 }
630
631 char **var_name;
632 if (num_nodal_fields > 0) {
633
634 var_name = malloc(num_nodal_fields * sizeof(char *));
635 for (INT j = 0; j < num_nodal_fields; j++) {
636 var_name[j] = malloc((MAX_STRING_LEN + 1) * sizeof(char));
637 snprintf(var_name[j], MAX_STRING_LEN + 1, "node_field_%" PRId64, j + 1);
638 }
639 err = ex_put_variable_names(exoid, EX_NODAL, num_nodal_fields, var_name);
640 if (err) {
641 fprintf(stderr, "after ex_put_variable_names (nodal), error = %d\n", err);
642 ex_close(exoid);
643 exit(-1);
644 }
645 for (INT j = 0; j < num_nodal_fields; j++) {
646 free(var_name[j]);
647 }
648 free(var_name);
649 }
650
651 if (num_global_fields > 0) {
652 globals = malloc(num_global_fields * sizeof(realtyp));
653 var_name = malloc(num_global_fields * sizeof(char *));
654 for (INT j = 0; j < num_global_fields; j++) {
655 var_name[j] = malloc((MAX_STRING_LEN + 1) * sizeof(char));
656 snprintf(var_name[j], MAX_STRING_LEN + 1, "global_field_%" PRId64, j + 1);
657 globals[j] = j;
658 }
659 err = ex_put_variable_names(exoid, EX_GLOBAL, num_global_fields, var_name);
660 if (err) {
661 fprintf(stderr, "after ex_put_variable_names (global), error = %d\n", err);
662 ex_close(exoid);
663 exit(-1);
664 }
665 for (INT j = 0; j < num_global_fields; j++) {
666 free(var_name[j]);
667 }
668 free(var_name);
669 }
670
671 if (num_element_fields > 0) {
672 free(elem_var_tab);
673 var_name = malloc(num_element_fields * sizeof(char *));
674 for (INT j = 0; j < num_element_fields; j++) {
675 var_name[j] = malloc((MAX_STRING_LEN + 1) * sizeof(char));
676 snprintf(var_name[j], MAX_STRING_LEN + 1, "element_field_%" PRId64, j + 1);
677 }
678 err = ex_put_variable_names(exoid, EX_ELEM_BLOCK, num_element_fields, var_name);
679 if (err) {
680 fprintf(stderr, "after ex_put_variable_names, error = %d\n", err);
681 ex_close(exoid);
682 exit(-1);
683 }
684 for (INT j = 0; j < num_element_fields; j++) {
685 free(var_name[j]);
686 }
687 free(var_name);
688 }
689
690 if (num_nodal_fields + num_global_fields + num_element_fields > 0) {
691 fprintf(stderr, "Domain %" PRId64 "/%" PRId64 ", Writing Timestep: ", i + 1, num_domains);
692 for (INT t = 0; t < num_timesteps; t++) {
693 realtyp time = t;
694 ex_put_time(exoid, t + 1, &time);
695 fprintf(stderr, "%" PRId64 ", ", t + 1);
696 if (num_global_fields > 0) {
697 err = ex_put_var(exoid, t + 1, EX_GLOBAL, 1, 0, num_global_fields, globals);
698 if (err) {
699 fprintf(stderr, "after ex_put_global_var, error = %d\n", err);
700 ex_close(exoid);
701 exit(-1);
702 }
703 }
704 for (INT j = 0; j < num_nodal_fields; j++) {
705 err = ex_put_var(exoid, t + 1, EX_NODAL, j + 1, 0, loc_num_nodes, x);
706 if (err) {
707 fprintf(stderr, "after ex_put_nodal_var, error = %d\n", err);
708 ex_close(exoid);
709 exit(-1);
710 }
711 }
712 for (INT j = 0; j < num_element_fields; j++) {
713 err = ex_put_var(exoid, t + 1, EX_ELEM_BLOCK, j + 1, EBLK_ID, loc_num_elements, x);
714 if (err) {
715 fprintf(stderr, "after ex_put_element_var, error = %d\n", err);
716 ex_close(exoid);
717 exit(-1);
718 }
719 }
720 }
721 fprintf(stderr, "\n");
722 }
723
724 err = ex_close(exoid);
725
726 if (err) {
727 fprintf(stderr, "after ex_close, error = %d\n", err);
728 exit(-1);
729 }
730 if (debug) {
731 fprintf(stderr, "\tFile written.\n");
732 }
733 }
734
735 /*
736 * Free Memory
737 */
738
739 if (num_domains > 1) {
740 free(domain_connect);
741 free(elem_map);
742 free(loc_connect);
743 free(loc_xcoords);
744 free(loc_ycoords);
745 free(loc_zcoords);
746 free(node_map);
747 }
748 if (num_global_fields > 0) {
749 free(globals);
750 }
751 }
752
753 /***********************************************************************
754 *
755 * Create element local/global map
756 *
757 * This puts contiguous groups of elements in each domain. This is
758 * a somewhat reasonable map for a realistic application.
759 *
760 ***********************************************************************/
761 void create_elem_map(INT loc_num_elems, INT elem_num, INT *elem_map, INT map_origin)
762 {
763 for (INT i = 0; i < loc_num_elems; i++) {
764 elem_map[i] = map_origin + elem_num++;
765 }
766 }
767
768 /***********************************************************************
769 *
770 * Extract current domain's connectivity, referencing global node ids
771 *
772 * This extracts the "domain connectivity," that is, the connectivity
773 * of the elements in the current domain. The node ids in the domain
774 * connectivity reference global node ids.
775 *
776 ***********************************************************************/
777
778 void extract_connect(INT element_offset, INT num_elem, INT *elem_map, INT *connect,
779 INT *domain_connect, INT map_origin)
780 {
781 INT i = element_offset;
782 INT m = 0;
783 for (INT j = 0; j < num_elem; j++) {
784 if (elem_map[j] == i + map_origin) { /* extract this element */
785 INT offset = (i * NUM_NODES_PER_ELEM);
786 for (INT k = offset; k < offset + NUM_NODES_PER_ELEM; k++) {
787 domain_connect[m++] = connect[k];
788 }
789 i++;
790 }
791 }
792 }
793
794 /***********************************************************************
795 *
796 * The local/global node map is just the current domain's connectivity,
797 * sorted, with duplicate entries removed. This isn't obvious, but
798 * trust me.
799 *
800 ***********************************************************************/
801 void create_node_map(INT len_map, INT len_connect, INT *domain_connect, INT *node_map,
802 INT *loc_num_nodes, INT map_origin)
803 {
804 for (INT i = 0; i < len_map; i++) {
805 node_map[i] = 0;
806 }
807
808 for (INT i = 0; i < len_connect; i++) {
809 node_map[domain_connect[i] - map_origin] = 1;
810 }
811
812 INT cnt = 0;
813 for (INT i = 0; i < len_map; i++) {
814 if (node_map[i] > 0) {
815 node_map[cnt++] = i + map_origin;
816 }
817 }
818 *loc_num_nodes = cnt;
819 }
820
821 /***********************************************************************
822 *
823 * Using local/global node map, convert the domain connectivity
824 * (referencing global node ids) to local connectivity (referencing
825 * local node ids).
826 *
827 * This requires inverting the local/global map, a relatively expensive
828 * operation. The procedure is:
829 *
830 * for every entry in the domain connectivity
831 * search the node map until found
832 * set the value of the entry in the local connectivity to
833 * the index of the located value in the node map
834 *
835 ***********************************************************************/
836 void create_local_connect(INT *node_map, INT len_node_map, INT len_connect, INT *domain_connect,
837 INT *loc_connect, INT map_origin)
838 {
839 for (INT i = 0; i < len_connect; i++) {
840 INT index = bin_search2(domain_connect[i], len_node_map, node_map);
841 if (index != -1) { /* found */
842 loc_connect[i] = index + map_origin;
843 }
844 else {
845 fprintf(stderr, "error creating local connectivity; i = %" PRId64 "\n", i);
846 exit(-1);
847 }
848 }
849 }
850
851 /*****************************************************************************
852 *
853 * Searches a monotonic list of values for the value, 'value'.
854 * It returns the index (0-based) of the first position found, which
855 * matches 'value'.
856 * The list is assumed to be monotonic, and consist of elements
857 * list[0], ..., list[n-1].
858 * If no position in list matches value, it returns the value -1.
859 *
860 *****************************************************************************/
861
862 INT bin_search2(INT value, INT num, INT List[])
863 {
864 INT bottom = 0;
865 INT top = num - 1;
866 while (bottom <= top) {
867 INT middle = (bottom + top) >> 1;
868 INT g_mid = List[middle];
869 if (value < g_mid) {
870 top = middle - 1;
871 }
872 else if (value > g_mid) {
873 bottom = middle + 1;
874 }
875 else {
876 return middle; /* found */
877 }
878 }
879 return -1;
880 } /* bin_search2 */
881
882 /*****************************************************************************/
883 void get_file_name(const char *base, const char *ext, int rank, int nprocs, const char *other,
884 char *output)
885 {
886
887 output[0] = '\0';
888 ex_copy_string(output, base, MAX_STRING_LEN);
889 strcat(output, ".");
890 strcat(output, ext);
891 if (other != NULL) {
892 strcat(output, ".");
893 strcat(output, other);
894 }
895 if (nprocs > 1) {
896 /*
897 * Find out the number of digits needed to specify the processor ID.
898 * This allows numbers like 01-99, i.e., prepending zeros to the
899 * name to preserve proper alphabetic sorting of the files.
900 */
901 INT iMaxDigit = 0, iMyDigit = 0;
902 INT iTemp1 = nprocs;
903 do {
904 iTemp1 /= 10;
905 iMaxDigit++;
906 } while (iTemp1 >= 1);
907
908 iTemp1 = rank;
909 do {
910 iTemp1 /= 10;
911 iMyDigit++;
912 } while (iTemp1 >= 1);
913
914 char cTemp[128];
915 snprintf(cTemp, 128, "%d", nprocs);
916
917 strcat(output, ".");
918 strcat(output, cTemp);
919 strcat(output, ".");
920
921 /*
922 * Append the proper number of zeros to the filename.
923 */
924 for (INT i1 = 0; i1 < iMaxDigit - iMyDigit; i1++) {
925 strcat(output, "0");
926 }
927
928 snprintf(cTemp, 128, "%d", rank);
929 strcat(output, cTemp);
930 }
931 }